我有一个UITableViewCell
和BEMCheckBox
,我想删除时没有自来水在这里按下电池是我的代码:从的tableView删除单元格内的UITableViewCell
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)
}
}
我得到follwing错误:
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).'
'MenuViewController()products.removeObject(forKey: “产品\(指数.row)”)'那是创造了一个全新的'MenuViewController'对象?你能检查对象是否被正确删除? – Larme