我有一個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