ios - 从过滤后的数组中在 tableView 中显示数据

标签 ios uitableview core-data swift2 xcode7

我编写了从 Core Data 过滤事件的代码,并且只打印日期属性等于在日历中选择的日期的事件。继承人的代码:

let predicate = NSPredicate(format: "eventPastStringDate = %@", formatter.stringFromDate(selectedDate))

//This prints the exact data that I want
print((eventsPast as NSArray).filteredArrayUsingPredicate(predicate))

这有效,它按照我的意愿过滤数据,并且只打印我想要的事件。问题是我不知道如何在 tableView 中显示这些数据。通常我可以在 cellForRowAtIndexPath 中像这样在 tableView 中显示来自核心数据的所有数据...

let ePast = eventsPast[indexPath.row]
let cellPast = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell

// Sets labels in cell to whatever user typed in on the events page
cellPast.titlePrototypeCell!.text = ePast.eventPastTitle!
cellPast.datePrototypeCell!.text = "Date: " + ePast.eventPastDateLabel!

return cellPast

...但我不确定如何像上面那样以数组形式访问新数据。有任何想法吗?

最佳答案

您将在您的类(class)中拥有第二个数组。当没有过滤器时,两个数组是相同的。当有过滤器时,您将拥有一个包含绝对所有内容的主数组,过滤后的数组是一个小子集。

(class var) myFilteredArray = (eventsPast as NSArray).filteredArrayUsingPredicate(predicate)

然后在你的tableview方法中:

let ePast = myFilteredArray[indexPath.row]

并确保将表行大小设置为过滤后的数组数,而不是主数组数。否则你将越界崩溃。

关于ios - 从过滤后的数组中在 tableView 中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38274969/

相关文章:

ios - 支付整合 payzee vs stripe

ios - 从 PFObject 数组中删除特定的 PFObject

cocoa-touch - 动画 UITableViewCell ContentView 在进入编辑模式时淡入淡出

ios - 按日期获取最后 25 个 CoreData 条目

ios - 使用我的 ViewController 作为 TabBarController

ios - Swift:重新启动游戏后多次调用方法

ios - 删除 UITableViewCell 后,出现空白行

ios - 如何在 tableview 单元格之间将数据从 firebase 传递到 View Controller ?

swift - 核心数据 EXC_BAD_ACCESS 删除时

ios - 核心数据 : Additional object inserted into Managed Object Context after restarting App