如何在 Realm Swift 中重新排序 UITableView 单元格?

Dan*_*iel 0 realm uitableview ios swift

我正在将Realm用于我的笔记应用程序。我以前使用过核心数据,现在正在将核心数据迁移到领域,但我遇到了麻烦!像这样重新排序对象会导致错误。

do {  
let realm = try Realm()
let source = myNote[sour!.row]        
try realm.write() {
 myNote.remove(objectAtIndex: sourceIndexPath!.row)
 myNote.insert(source, at: destensionIndexPath!.row)
 }
 }
 catch {
 print("handle error")
 }
Run Code Online (Sandbox Code Playgroud)

所以我向我的对象添加了 orderPosition属性

dynamic var orderPosition: Int = 0
Run Code Online (Sandbox Code Playgroud)

并将tableView moveRowAtIndexPath更改为此 ReorderingRealmResultsInTableView.swift

但这并没有多大帮助。那么如何重新排序领域中的对象

jps*_*sim 5

我鼓励您将订购的项目存储在一个List而不是基于orderPosition属性的排序中。

移动项目时手动存储索引的性能会低得多,因为“旧索引”和“新索引”之间的所有对象都需要进行变异以考虑更改。

然后,您可以使用List.move(from:to:)将对象从一个索引移动到另一个索引,这应该直接对应于您重新排序的表视图中的索引。

这是您可以遵循的教程,指导您构建任务管理应用程序,包括对重新排序任务的支持:https : //realm.io/docs/realm-mobile-platform/example-app/cocoa/