我得到這個錯誤「只能從它屬於的領域刪除一個對象」每次我嘗試從我的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])
}
是怎麼回事的任何想法? 在此先感謝!