【发布时间】:2015-02-05 11:17:11
【问题描述】:
例如,我有一个名为 Tasks 的 Entity 和一个名为 date 的 Attribute 和一个填充此 Tasks 的 UITableView Entity.
这是我目前看到的:
7-Dec-14 09:30
7-Dec-14 11:00
7-Dec-14 13:30
7-Dec-14 16:00
现在让我们假设当前时间实际上是 14 年 12 月 7 日 12:00,所以前 2 行已经过去,但后 2 行是未来时间。我想将这些分成UITableView 中的组。
我发现我可以在实体上创建一个瞬态属性,如下所示:
var dateGroup: NSNumber {
get {
if date.compare(NSDate()) == NSComparisonResult.OrderedAscending {
return 0
} else {
return 1
}
}
}
当我设置 sectionNameKeyPath: "dateGroup":
Group 0
7-Dec-14 09:30
7-Dec-14 11:00
Group 1
7-Dec-14 13:30
7-Dec-14 16:00
我的麻烦是如何让UITableView 实际显示这样的结果(所以未来的任务高于过期任务):
Group 1
7-Dec-14 13:30
7-Dec-14 16:00
Group 0
7-Dec-14 09:30
7-Dec-14 11:00
我尝试在瞬态属性 (dateGroup) 上创建 NSSortDescriptor,但这不起作用。
我意识到这是因为我的NSSortDescriptor 在“日期”属性上。但我不知道如何搜索 date > currentDate 的行,然后是 date
我想基本上我想要做的是交换如果有超过 1 个部分。
希望这是有道理的。您将如何解决这个排序问题?
更新
好的,我已将其完全重写为使用 2 x FetchRequestControllers self.upcomingFRC(第 0 节)和 self.elapsedFRC(第 1 节),以下内容现在可以正常工作,没有任何错误或 nils。
FetchResultsControllers 之间的所有插入、更新、移动和删除都流畅且具有动画效果。
非常感谢 pbasdf。
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
println("insert")
if var indexPathNew = newIndexPath {
if controller == self.elapsedFRC { indexPathNew = NSIndexPath(forRow: newIndexPath!.row, inSection: 1) }
println("N indexPath : \(indexPathNew)")
self.tableViewEvent.insertRowsAtIndexPaths([indexPathNew], withRowAnimation: .Fade)
}
case .Delete:
println("delete")
if var indexPathOld = indexPath {
if controller == self.elapsedFRC { indexPathOld = NSIndexPath(forRow: indexPath!.row, inSection: 1) }
println("O indexPath : \(indexPathOld)")
self.tableViewEvent.deleteRowsAtIndexPaths([indexPathOld], withRowAnimation: .Fade)
}
case .Update:
println("update")
if self.showLarge {
if var indexPathOld = indexPath {
if controller == self.elapsedFRC { indexPathOld = NSIndexPath(forRow: indexPath!.row, inSection: 1) }
println("O indexPath : \(indexPathOld)")
if let largeCell = self.tableViewEvent.cellForRowAtIndexPath(indexPathOld) as? LargeTableViewCell {
if indexPathOld.section == 0 { self.configureLargeCell(largeCell, frc:self.upcomingFRC, row:indexPathOld.row) }
else if indexPathOld.section == 1 { self.configureLargeCell(largeCell, frc:self.elapsedFRC, row:indexPathOld.row) }
} else {
println("******************************************")
println("found nil largeCell - configure Large Cell")
println("******************************************")
}
} else {
println("******************************************")
println("found nil indexPath - configure Large Cell")
println("******************************************")
}
} else {
if let indexPathOld = indexPath {
if let smallCell = self.tableViewEvent.cellForRowAtIndexPath(indexPathOld) as? SmallTableViewCell {
if indexPathOld.section == 0 { self.configureSmallCell(smallCell, frc:self.upcomingFRC, row:indexPathOld.row) }
else if indexPathOld.section == 1 { self.configureSmallCell(smallCell, frc:self.elapsedFRC, row:indexPathOld.row) }
} else {
println("******************************************")
println("found nil smallCell - configure Small Cell")
println("******************************************")
}
} else {
println("******************************************")
println("found nil indexPath - configure Small Cell")
println("******************************************")
}
}
case .Move:
println("move")
if var indexPathOld = indexPath {
if controller == self.elapsedFRC { indexPathOld = NSIndexPath(forRow: indexPath!.row, inSection: 1) }
println("O indexPath : \(indexPathOld)")
self.tableViewEvent.deleteRowsAtIndexPaths([indexPathOld], withRowAnimation: .Fade)
}
if var indexPathNew = newIndexPath {
if controller == self.elapsedFRC { indexPathNew = NSIndexPath(forRow: newIndexPath!.row, inSection: 1) }
println("N indexPath : \(indexPathNew)")
self.tableViewEvent.insertRowsAtIndexPaths([indexPathNew], withRowAnimation: .Fade)
}
default:
return
}
}
【问题讨论】:
-
我的错误 - 您应该使用 indexPath 而不是 newIndexPath 将单元格出列(我会修改我的答案)。使用替代的 dequeue(不指定 indexPath)应该可以正常工作,但是您需要仔细检查您没有得到 nil 单元格(而
dequeue... forIndexPath:如果找不到原型单元格或注册的类/笔尖,则会抛出异常) .
标签: ios sorting core-data swift transient