【发布时间】: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