【问题标题】:How do I reorder UITableView cells in Realm Swift?如何在 Realm Swift 中重新排序 UITableView 单元格?
【发布时间】:2016-10-18 05:49:38
【问题描述】:

我将 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")
 }

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

dynamic var orderPosition: Int = 0

并将 tableView moveRowAtIndexPath 更改为此 ReorderingRealmResultsInTableView.swift

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

【问题讨论】:

    标签: ios swift uitableview realm


    【解决方案1】:

    我鼓励您将订购的商品存储在 List 中,而不是根据 orderPosition 属性进行排序。

    在移动项目时,手动存储索引的性能会大大降低,因为“旧索引”和“新索引”之间的所有对象都需要进行变异以适应变化。

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

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

    【讨论】:

      【解决方案2】:

      List 确实高效且干净,但我不确定如何通过服务器同步它。所以在我的情况下,我使用orderPosition: Double,并且该值被计算为插入对象的两个现有orderPositions 的中间。另外请记住,您可以在不更新通知中的 tableView 的情况下执行写入:try! list.realm?.commitWrite(withoutNotifying: [notificationToken!])

      【讨论】:

        【解决方案3】:

        正如其他人所建议的,List 是解决方案。这是在 Swift 5 上实现该方法的示例:

        import UIKit
        import RealmSwift
        
        // The master list of `Item`s stored in realm
        class Items: Object {
            @objc dynamic var id: Int = 0
            let items = List<Item>()
        
            override static func primaryKey() -> String? {
                return "id"
            }
        }
        
        class Item: Object {
            @objc dynamic var id: String = UUID().uuidString
            @objc dynamic var name = ""
        }
        
        class ViewController: UITableViewController {
            let realm = try! Realm()
            var items = RealmSwift.List<Item>()
        
            override func viewDidLoad() {
                super.viewDidLoad()
        
                // initialize database
                var itemsData = realm.object(ofType: Items.self, forPrimaryKey: 0)
                if itemsData == nil {
                    itemsData = try! realm.write { realm.create(Items.self, value: []) }
                }
                items = itemsData!.items
        
                // temporarily add new items
                let newItem1 = Item()
                newItem1.name = "Item 1"
                let newItem2 = Item()
                newItem2.name = "Item 2"
                try! realm.write {
                    items.append(newItem1)
                    items.append(newItem2)
                }
            }
        
            func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                return items.count
            }
        
            ...
        
            func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
                return true
            }
        
            func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
                try! items.realm?.write {
                    items.move(from: sourceIndexPath.row, to: destinationIndexPath.row)
                }
            }
        }
        

        您可以在 GitHub Repo (https://github.com/realm/realm-cocoa) 的 examples/ 下找到适用于 iOS 和 macOS 的示例应用程序,演示如何使用 Realm 的许多功能,如迁移、如何将其与 UITableViewControllers、加密、命令行工具一起使用还有更多。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-21
          • 1970-01-01
          • 1970-01-01
          • 2015-10-30
          • 1970-01-01
          相关资源
          最近更新 更多