【发布时间】:2017-01-28 08:59:51
【问题描述】:
我正在使用 Xcode 8 和 swift 3。
当我尝试从 UITableView 中删除一行时遇到断言失败。
-[UITableView _endCellAnimationsWithContext:] 中的断言失败,/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3599.5/UITableView.m:1610
由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第0节中的行数无效。更新后现有节中包含的行数(25)必须等于行数更新前包含在该节中的行数 (25),加上或减去从该节插入或删除的行数(0 插入,1 删除),加上或减去移入或移出该节的行数(0 移入, 0 移出)。'
代码:
// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// create a new cell if needed or reuse an old one
let cell:UserCell = self.tableView.dequeueReusableCell(withIdentifier: "tblCell") as! UserCell!
cell.titleLabel.text = self.animals[(indexPath as NSIndexPath).row]
cell.btnDelete.addTarget(self, action:#selector(ViewController.buttonClicked(sender:)), for: UIControlEvents.touchUpInside);
return cell
}
func buttonClicked(sender:UIButton) {
if let superview = sender.superview {
if let cell = superview.superview as? UserCell {
if let indexPath = self.tableView.indexPath(for: cell){
print("row = ",indexPath.row)
self.tableView.beginUpdates()
self.tableView.deleteRows(at: [indexPath], with: .left)
self.tableView.endUpdates()
self.animals.remove(at:indexPath.row)
}
}
}
}
我刚刚开始学习 swift,所以请帮助我。
【问题讨论】:
-
在结束更新之前必须刷新数据源。尝试在
self.tableView.endUpdates()之前调用self.animals.remove(at:indexPath.row) -
@pedrouan "deleteRowsAtIndexPaths" 在 swift 3 中不存在。
标签: ios swift uitableview swift3