【问题标题】:Trying to edit a List in realm尝试在领域中编辑列表
【发布时间】:2018-12-27 12:32:33
【问题描述】:

我正在尝试使用领域创建List

class TripsList : Object {
    let trips = List<Trip>()
}

然后,在我的 ViewController 类中:

var trips : Results<TripsList>?

override func viewDidLoad() {
    super.viewDidLoad()

    trips = realm.objects(TripsList.self)

}

当有人移动 UITableViewRow 时,我想更新我的领域数据库。

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    let movedObject = self.realm.objects(Trip.self)[sourceIndexPath.row]

    trips.remove(at: sourceIndexPath.row)
    trips.insert(movedObject, at: destinationIndexPath.row)

}

这是我的 TableView 数据源方法:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return realm.objects(Trip.self).count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    cell.textLabel?.font = UIFont.systemFont(ofSize: 17)
    cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
    cell.textLabel?.text = nameData.names[realm.objects(Trip.self)[indexPath.row].tripID]
    return cell
}

问题是没有选项可以做trips.remove(at:)trips.insert(_:at:)

我的总体目标是能够在有人移动 UITableViewRow 并更新我的领域数据库时插入和删除。

【问题讨论】:

    标签: swift realm realm-list


    【解决方案1】:

    您不能直接修改Results 实例。 Results 是自动更新集合,这意味着它们始终反映您用于初始化它们的查询的当前状态。您需要修改Realm 才能修改Results 实例。

    此外,Results 实例仅保证在您对其进行排序时保持其排序。因此,您需要在 Trip 上引入一个属性,用于对对象进行排序并在用户移动一行时修改该属性。

    您的TripsList 类似乎是不必要的,因为您似乎只是想在Realm 中存储许多Trip 对象,然后在不实际进行任何分组的情况下检索它们。即使您需要对它们进行分组,也可以使用 Realm 查询来实现。请记住这一点,这就是我修改您当前代码以允许用户对其Trips 进行排序并将排序保存到Realm 的方式。

    class Trip: Object {
        // Your existing code for Trip 
        ...
        // Sorting property
        @objc dynamic var sortingIndex = 0
    }
    

    在您的表格视图控制器中:

    var trips : Results<Trip>?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        trips = realm.objects(Trip.self).sorted(byKeyPath: "sortingIndex")
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return trips?.count ?? 0
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.font = UIFont.systemFont(ofSize: 17)
        cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
        if let tripID = trips?[indexPath.row].tripID {
            cell.textLabel?.text = nameData.names[tripID]
        }
        return cell
    }
    
    override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        guard let movedObject = trips?.[sourceIndexPath.row] else { return }
        // Depending on your exact needs, you might want to update the `sortingIndex` property of your other rows as well, whose position in the table view was affected by the reordering
        try! realm.write {
            movedObject.sortingIndex = destinationIndexPath.row
        }
    }
    

    【讨论】:

    • 能否提供一些示例代码供我使用?这听起来有点令人困惑。
    • @Callum 如果您使用UITableViewDataSource 方法更新您的问题,当然可以。在没有更多上下文的情况下,您混合使用 TripsList,它包含一个 List&lt;Trip&gt;,然后简单地使用 self.realm.objects(Trip.self)[sourceIndexPath.row] 查询所有 Trips 有点误导
    • 你去吧,@Dávid Pásztor
    • @Callum 在我看来,您实际上并没有将TripsList 用于任何事情,而且您实际上也不需要它。您是否只想将 Trip 对象存储在 Realm 中并在表格视图中显示所有行程,用户可以重新排列?你想用TripsList 做什么?如果是这种情况(您实际上不需要TripsList,我将使用示例代码更新我的答案)。
    • 我做了一些研究,看起来我需要创建一个新类来存储列表。谢谢,代码会很有帮助。
    猜你喜欢
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多