我有一个表格 View ,我在其中使用 SwipeCellkit 来删除自定义单元格。当按下删除按钮时,我删除了 Realm 中的指定对象, 但是,我没有重新加载 tableview,但 tableview 行仍然已被删除。我想知道它是如何完成的(realm 是否正在执行隐式 tableview 重新加载?)
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAt: indexPath)
cell.textLabel?.text = categoryArray?[indexPath.row].name
return cell
}
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
// handle action by updating model with deletion
self.updateModel(at: indexPath)
}
// customize the action appearance
deleteAction.image = UIImage(named: "delete-icon")
return [deleteAction]
}
func updateModel(at indexPath: IndexPath) {
if let category = self.categoryArray?[indexPath.row]
{
do{
try self.realm.write {
self.realm.delete(category)
}
}catch{
print("eror deleting object")
}
}
}
最佳答案
这与 Realm
无关。
这是因为您在 editActionsForRowAtIndexPath
方法中返回的 deleteAction
。
更准确地说,SwipeActionStyle.destructive
负责创建删除操作。
如 docs 中所述:
The built-in .destructive, and .destructiveAfterFill expansion styles are configured to automatically perform row deletion when the action handler is invoked (automatic fulfillment).
希望对你有帮助
关于ios - 当我从 Realm 中删除一个对象时,tableview 是如何隐式重新加载的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57737391/