【发布时间】:2017-05-09 04:01:26
【问题描述】:
每次我尝试从我的 tableview 上的领域中删除一个对象时,我都会收到此错误“只能从它所属的领域中删除一个对象”。以下是相关代码:
let realm = try! Realm()
var checklists = [ChecklistDataModel]()
override func viewWillAppear(_ animated: Bool) {
checklists = []
let getChecklists = realm.objects(ChecklistDataModel.self)
for item in getChecklists{
let newChecklist = ChecklistDataModel()
newChecklist.name = item.name
newChecklist.note = item.note
checklists.append(newChecklist)
}
tableView.reloadData()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return checklists.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ChecklistCell", for: indexPath) as! ListsTableViewCell
cell.name.text = checklists[indexPath.row].name
return cell
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
try! realm.write {
realm.delete(checklists[indexPath.row])
}
//delete locally
checklists.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .fade)
}
}
我知道具体来说就是这部分:
// Delete the row from the data source
try! realm.write {
realm.delete(checklists[indexPath.row])
}
对正在发生的事情有任何想法吗? 提前致谢!
【问题讨论】:
标签: swift uitableview realm