删除 tableView 中的一行工作正常,直到它是最后一个现有行。然后我崩溃了:“由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第0节中的行数无效。更新后现有节中包含的行数(1)必须等于更新前该节中包含的行数 (1),加上或减去从该节插入或删除的行数(0 插入,1 删除),加上或减去移入或移出该节的行数( 0 人搬进来,0 人搬出去)。”
我知道单元格的数量与预期的数量不同,并且在此函数中:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if categoryArray?.count == 0 { // <<-- i imagine this line cause problem, though deleting it will not let me show "no categories made yet" and while it would be better to use "if let" statement i make "array" in realm which never is nil as its being created with 0 elements(which is not nil)
return 1
}
return categoryArray?.count ?? 1
}
问题是我如何才能以删除工作的方式实现这一点,而且我仍然可以显示一种“替换”单元格,它将显示而不是空的 tableView。
这是整个滑动删除功能:
扩展分类视图控制器:SwipeTableViewCellDelegate {
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
guard orientation == .right else { return nil }
let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
do{
try self.realm.write{
self.realm.delete((self.categoryArray?[indexPath.row])!)
}
} catch {
print("Error deleting category\(error)")
}
}
return [deleteAction]
}
任何意见,将不胜感激