我有一个UITableViewCell
和一个BEMCheckBox
,我想在按下did tap时删除单元格,这是我的代码:
func didTap(_ checkBox: BEMCheckBox) {
let index = tableView?.indexPath(for: self)
MenuViewController().products.removeObject(forKey: "Product\(index!.row)") // removes the cell from the database of the tableview
tableView?.deleteRows(at: [index!], with: .fade)
}
这是我获取tableView
的方法:
extension UIView {
func parentView<T: UIView>(of type: T.Type) -> T? {
guard let view = self.superview else {
return nil
}
return (view as? T) ?? view.parentView(of: T.self)
}
}
extension UITableViewCell {
var tableView: UITableView? {
return self.parentView(of: UITableView.self)
}
}
我收到以下错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
最佳答案
与您的数据源有关。您的数据源未相应更新。您应该在重新加载 UITableview
之前检查您的数据源。这是由于数据项计数不匹配而发生的。
关于swift - 从 UITableViewCell 内的 tableView 中删除单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43495091/