swift - 尝试在领域中编辑列表
问题描述
我正在尝试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 并更新我的领域数据库时插入和删除。
解决方案
您不能直接修改Results
实例。Results
是自动更新集合,这意味着它们始终反映您用于初始化它们的查询的当前状态。您需要修改Realm
才能修改Results
实例。
此外,Results
只有在您对其进行排序时,才能保证实例保持其排序。因此,您需要引入一个属性Trip
,用于对对象进行排序并在用户移动一行时修改该属性。
您的TripsList
类似乎是不必要的,因为您似乎只是想Trip
在 Realm 中存储许多对象,然后在没有实际进行任何分组的情况下检索它们。即使您需要对它们进行分组,也可以使用 Realm 查询来实现。记住这一点,这就是我修改当前代码以允许用户对其Trip
s 进行排序并将排序保存到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
}
}
推荐阅读
- javascript - 添加 toast 通知时如何解决以下错误?
- javascript - 将第二个参数从视图中的模型传递给 AJAX 函数
- spring-boot - 在 Thymeleaf 生成的 HTML 文档中包含一个
- laravel - 无法与主机 smtp.googlemail.com 建立连接 [连接尝试失败
- r - 大型栅格的最佳 wopt 选项
- python - 泛型函数的类型提示?
- solidity - 免费 TON tonos-cli。无法部署合约。错误:创建入站消息失败:编码部署消息失败:参数计数错误
- python - 如何保存
作为 png 文件 - javascript - 如何在不解除它的情况下让父母访问儿童状态?
- kotlin - Kotlin setters/getters 元编程问题