我编写了从 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/