我正在尋找動畫從我的表視圖中刪除項目。我正在使用CoreData獲取的結果控制器。我遇到的問題是引發行數必須與項數匹配的異常。動畫刪除行與提取結果控制器
這裏的錯誤,當我刪除我得到:
原因:「無效的更新:在第0無效數行的更新後, 數包含在現有的部分行(4) 必須等於 update(3)之前該節中包含的行數,加上或減去從該節插入或刪除 (插入0,刪除0)和加上或減去行數 的行數移入或移出該部分(0移入,0移出)。'
而這裏的代碼:
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let context = appDelegate.persistentContainer.viewContext
if editingStyle == .delete {
// Delete the row from the data source
context.delete(fetchedResultsController.object(at: indexPath))
do {
try context.save()
// Animate deleted row
tableView.deleteRows(at: [indexPath], with: .fade)
} catch {
print("There was an error: \(error)")
}
}
}
謝謝。
對,這就是我通常所做的,但是它看起來像'fetchedResultsController'不允許我更新抓取的對象屬性。抓取結果控制器應該處理更新數據源(它會這樣做,但不會生成動畫)。 – DavidVII
你嘗試過從NSFetchedResultsControllerDelegate動畫tableview嗎? 這裏有一個例子(在Objective-C中):https://developer.apple.com/documentation/coredata/nsfetchedresultscontrollerdelegate – Dule
我還沒有嘗試過,但我會給它一個鏡頭,看看。我相當新,所以我必須看看我是否可以將其轉換爲快速。 – DavidVII