Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用UITableView-Swift从域中删除对象_Ios_Swift_Uitableview_Realm - Fatal编程技术网

Ios 如何使用UITableView-Swift从域中删除对象

Ios 如何使用UITableView-Swift从域中删除对象,ios,swift,uitableview,realm,Ios,Swift,Uitableview,Realm,通过UITableView从Realm删除对象的最常见方式(代码结构)是什么 以下代码可以很好地在UITableView中显示领域中的数据,但如果我需要删除一行并更新领域则不行,因为结果没有删除方法 我是否需要将我的对象放入列表中,并通过它执行删除操作?如果这是最常用的方法,我不太确定如何保持“列表”和领域的结果持续同步 模范班 主视图控制器 同样,通过UITableView从Realm删除对象最常见的方法是什么 谢谢为了简洁起见省略了一些展开和捕获逻辑 func tableView(_ tab

通过
UITableView
Realm
删除对象的最常见方式(代码结构)是什么

以下代码可以很好地在UITableView中显示
领域
中的数据,但如果我需要删除一行并更新
领域
则不行,因为
结果
没有
删除
方法

我是否需要将我的对象放入
列表中
,并通过它执行删除操作?如果这是最常用的方法,我不太确定如何保持“列表”和
领域的
结果
持续同步

模范班 主视图控制器 同样,通过
UITableView
Realm
删除对象最常见的方法是什么


谢谢

为了简洁起见省略了一些展开和捕获逻辑

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCellEditingStyle.delete{
        if let item = items?[indexPath.row] {
            try! realm.write {
                realm.delete(item)
            }
            tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
        }
    }
}

您还可以实现此块:

   if let item = items?[indexPath.row] {
                do {
                    try realm.write {
                        realm.delete(item)
                    }
                } catch {
                    print("Error deleting item, \(error)")
                }
                tableView.reloadData()
            }

哦,我明白了,
领域
类,哎呀。谢谢!快速提问,您知道如何在
结果的开头添加/插入项目,而不是在最后添加/插入项目(默认设置)?换句话说,我希望总是在[0]索引处插入项。类似于<代码>试试!realm.write{realm.insert(item!,位于:0)}
,但当然
realm
没有插入方法。这是另一个问题-我建议在单独的线程中询问。因此,管理人员不赞成不相关的后续行动。如果你问我并给我贴标签,我很乐意回答。@d莫罗-这是新问题。非常感谢你的帮助。
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCellEditingStyle.delete{
        items!.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
    }
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCellEditingStyle.delete{
        if let item = items?[indexPath.row] {
            try! realm.write {
                realm.delete(item)
            }
            tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
        }
    }
}
   if let item = items?[indexPath.row] {
                do {
                    try realm.write {
                        realm.delete(item)
                    }
                } catch {
                    print("Error deleting item, \(error)")
                }
                tableView.reloadData()
            }