繁体   English   中英

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

[英]How do I reorder UITableView cells in Realm 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")
 }

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

dynamic var orderPosition: Int = 0

并将tableView moveRowAtIndexPath更改为此ReorderingRealmResultsInTableView.swift

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

我鼓励您将有序项目存储在List而不是根据orderPosition属性进行排序。

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

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

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

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

正如其他人所建议的,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 一起使用,加密、命令行工具等等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM