【发布时间】:2017-04-21 08:39:57
【问题描述】:
当我试图删除从 Core Data 的 NSManagedObject 派生的 Product 类型的对象时。对象可以成功移除。但它在tableView.deleteRows(at:with:) 的行上崩溃了。
所以,每次它崩溃,我再次打开应用程序,对象被成功删除,但我只是不知道它为什么在tableView.deleteRows(at:with:)崩溃。
我该如何解决?
class ProductListInSection {
let sectionName: String
var productsInSection: [Product]
init?(sectionName: String, productInSection: [Product] ){
guard !sectionName.isEmpty else {
return nil
}
self.sectionName = sectionName
self.productsInSection = productInSection
}
}
var categorySections: [ProductListInSection]
// ...
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
let element = self.categorySections[indexPath.section].productsInSection[indexPath.row]
AppDelegate.viewContext.delete(element)
do {
try AppDelegate.viewContext.save() // successfully removed.
} catch {
print("Fail: \(error)")
return
}
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) // crashed here.
}
}
以下是错误信息:
2017-04-21 15:54:42.159 POS[20241:2852960] *** 断言失败 -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit -3600.7.47/UITableView.m:1737
【问题讨论】:
标签: swift uitableview core-data