可以在UITableViewDelegate协议的方法中实现将被删除单元格对应的Core Data实体重置的代码。 例如,在以下协议方法中实现:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let context = appDelegate.persistentContainer.viewContext
let item = fetchedResultsController.object(at: indexPath)
context.delete(item)
// 重置被删除的Core Data实体
// ...
do {
try context.save()
} catch {
print("Error deleting item: \(error.localizedDescription)")
}
}
}
在上述示例中,当单元格被删除时,会调用UITableViewDelegate协议中的commit editingStyle方法。在此方法中,可以获取被删除的Core Data实体并删除它。接下来,可以在“重置被删除的Core Data实体”的代码块中实现对此实体的重置操作。最后,保存上下文以确保更改被持久化。
上一篇:表格视图单元格未显示相应的网站