ios - 当我从 Realm 中删除一个对象时,tableview 是如何隐式重新加载的?

标签 ios swift realm

我有一个表格 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/

相关文章:

ios - 在 iOS 中高效同步数据

iphone - 无法在用户不可交互的 UIWebView 中激活链接

swift - 使用 UITextField 字符串更改 UIView 颜色

ios - 当我的应用程序处于后台或关闭状态时,远程推送通知到达时从负载获取数据

ios - 如何将attributedText UITextView保存到数据库

android - 此方法仅在托管模式下可用

ios - UIWebview 视频播放崩溃 - 仅限于 HTML 中的 jw 播放器

iOS 等待画面

c# - 将 Swift 完成闭包转换为 C#

xamarin - Realm dotnet : get element by index from RealmResults