【发布时间】:2017-11-27 23:22:30
【问题描述】:
我有一个UITableView,它有两个部分。每个部分显示查询特定领域对象的结果。
var contentRequests: Results<UESocketPostRequest>? {
return try? CustomRealm.realm().objects(UESocketPostRequest.self).filter("completed = false AND content != nil").sorted(byKeyPath: "content.createdAt", ascending: false)
}
var mediaUploads: Results<UEMediaUploadRequest>? {
return try? CustomRealm.realm().objects(UEMediaUploadRequest.self).filter("finished = false").sorted(byKeyPath: "media.content.createdAt", ascending: false)
}
我的 UITableViewDelegate 和 UITableViewDataSource 一致性:
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return self.contentRequests?.count ?? 0
case 1:
return self.mediaUploads?.count ?? 0
default:
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "UPLOADCELL") as? UEUploadCell else {
return UITableViewCell()
}
switch indexPath.section {
case 0:
if let request = self.contentRequests?[indexPath.row] {
cell.softBindSocketRequest(request)
}
case 1:
if let request = self.mediaUploads?[indexPath.row] {
cell.softBindMediaUploadRequest(request)
}
default:
break
}
return cell
}
然后我为我的每个领域结果设置NotificationToken,并像这样更新表格视图:
func startContentToken() {
self.contentToken = self.contentRequests?.observe({ [weak self] (changes) in
switch changes {
case .error:
break
case .initial:
let indexSet = IndexSet(integer: 0)
self?.tableView.reloadSections(indexSet, with: .automatic)
case .update(let content, deletions: let deletions, insertions: let insertions, modifications: let modifications):
self?.tableView.beginUpdates()
self?.tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 0) }),
with: .automatic)
self?.tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 0)}),
with: .automatic)
self?.tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 0) }),
with: .automatic)
self?.tableView.endUpdates()
}
})
}
func startMediaToken() {
self.mediaToken = self.mediaUploads?.observe({ [weak self] (changes) in
switch changes {
case .error:
break
case .initial:
let indexSet = IndexSet(integer: 1)
self?.tableView.reloadSections(indexSet, with: .automatic)
case .update(let media, deletions: let deletions, insertions: let insertions, modifications: let modifications):
self?.tableView.beginUpdates()
self?.tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 1) }),
with: .automatic)
self?.tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 1)}),
with: .automatic)
self?.tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 1) }),
with: .automatic)
self?.tableView.endUpdates()
}
})
}
由于此日志消息或类似内容封装的UITableView 不一致异常,我经常崩溃:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
是我做错了什么还是 Realm 不适合以这种方式与分组 UITableView 一起使用?
【问题讨论】:
标签: swift uitableview realm