【发布时间】:2016-06-16 15:56:32
【问题描述】:
Realm Collection Notifications 在使用 'map' 与 UITableView 行进行映射时可以正常工作。我如何通过将其映射到 UITableView 部分来实现相同的效果。
对于行,我遵循以下代码:
notificationToken = results.addNotificationBlock { [weak self] (changes: RealmCollectionChange) in
guard let tableView = self?.tableView else { return }
switch changes {
case .Initial:
tableView.reloadData()
break
case .Update(_, let deletions, let insertions, let modifications):
tableView.beginUpdates()
tableView.insertRowsAtIndexPaths(insertions.map { NSIndexPath(forRow: $0, inSection: 0) },
withRowAnimation: .Automatic)
tableView.deleteRowsAtIndexPaths(deletions.map { NSIndexPath(forRow: $0, inSection: 0) },
withRowAnimation: .Automatic)
tableView.reloadRowsAtIndexPaths(modifications.map { NSIndexPath(forRow: $0, inSection: 0) },
withRowAnimation: .Automatic)
tableView.endUpdates()
break
case .Error(let error):
// An error occurred while opening the Realm file on the background worker thread
fatalError("\(error)")
break
}
}
对于部分,我使用:
tableview.beginUpdates()
for insertIndex in insertions {
tableview.insertSections(NSIndexSet(index: insertIndex), withRowAnimation: .Automatic)
}
for deleteIndex in deletions {
tableview.deleteSections(NSIndexSet(index: deleteIndex), withRowAnimation: .Automatic)
}
for reloadIndex in modifications {
tableview.reloadSections(NSIndexSet(index: reloadIndex), withRowAnimation: .Automatic)
}
tableview.endUpdates()
这行得通。
但我想了解“地图”以及如何使用它来绘制部分地图。
tableView.insertSections(insertions.map { NSIndexSet(index: $0) }, withRowAnimation: .Automatic)
还有,
tableview.insertSections(insertions.map({ (index) -> NSIndexSet in
NSIndexSet(index: index)
}), withRowAnimation: .Automatic)
但是,两者都给了我同样的错误
“map”产生“[T]”,而不是预期的上下文结果类型“NSIndexSet”
【问题讨论】:
标签: ios swift uitableview realm