swift - Deleted tableviewcell gets replaced by last cell
问题描述
I've got a problem with my tableView that is filled with realm objects. When I delete a cell the last cell of the tableView is jumping into the place of the removed cell. Anybody knows what could be the reason for this behavior?
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete{
self.updateModel(at: indexPath)
self.loadTimes()
}
}
func loadTimes(){
results = realm.objects(Times.self)
tableView.reloadData()
}
func updateModel(at indexpath: IndexPath) {
if let itemForDeletion = results?[indexpath.row] {
do{
try self.realm.write {
self.realm.delete(itemForDeletion)
}
} catch {
print("Error saving context \(error)")
}
}
}
解决方案
Realm 仅在对Results
查询进行排序时才保证顺序一致。尝试添加呼叫sorted
results = realm.objects(Times.self)
变成
results = realm.objects(Times.self).sorted(byKeyPath: "time")
推荐阅读
- c# - 使用 IComparer 获取 DateTime 大于的索引
- twitter - 使用授权标头中的承载令牌将用户添加到自定义受众
- r - 我是在处理一个充满数字、因子、字符还是什么的表格?
- php - 如何解决 php 联系表单中的 hCaptcha 验证错误
- angular - ngClass 结合多个条件?
- c - 堆栈结构与堆结构有什么区别?
- sql - 如何组合和求和结果值直到列中的新值
- postgresql - PostgreSQL 通过 WHERE 查询数组中的嵌套对象
- r - 文件错误(文件,ifelse(追加,“a”,“w”)):无法打开连接另外:警告消息:在文件(文件,ifelse(追加,“a”,“w”)):
- c++ - 按字母顺序按两个标准对向量内的结构进行排序