【发布时间】:2015-11-05 01:53:31
【问题描述】:
我试图从我的 TableView 中删除一个项目,但是当我向左滑动并单击 Delete 时,我收到了这个错误:
这是我拥有的代码,当我评论 self.tableView.deleteRowsAtIndexPaths 行时,一切正常。
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
if let index: NSIndexPath = indexPath {
self.tableView.beginUpdates()
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
self.configurations.delete(ProductDatabase.deleteProduct(self.products[index.row]))
loadProductsToListOrder()
self.tableView.endUpdates()
}
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
我在堆栈溢出中搜索并找到了关于此beginUpdates 和endUpdates 的信息。
谢谢。
更新
loadProductsToListOrder 函数
func loadProductsToListOrder() {
self.products = ProductDatabase.listProduct(self.configurations.db!)
self.tableView.reloadData()
}
如果我的 TableView 中有一个元素,我就没有这个问题。如果我的 TableView 中有多个元素,就会发生这种情况。
更新 2
@harshayarabarla 给出的答案对我有用,因为我忘记在 deleteRowsAtIndex 之前添加self.products.removeAtIndex(indexPath.row)。
谢谢!
【问题讨论】:
-
你的应用崩溃了!!!你有没有崩溃日志?放置异常断点以查看崩溃的位置
-
显示方法加载产品!!!
-
func loadProductsToListOrder() { self.products = ProductDatabase.listProduct(self.configurations.db!) self.tableView.reloadData() }
-
你试过
self.tableView.reloadData()而不是beginUpdates和endUpdates()吗? -
我跑了断点,它发生在
self.tableView.endUpdates之后
标签: ios swift uitableview