swift - 从 UITableViewCell 内的 tableView 中删除单元格

标签 swift xcode uitableview

我有一个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/

相关文章:

ios - 使 Swift 数组符合协议(protocol)

ios - Swift - 以编程方式实例化 View Controller 时缺少元素

ios - 'RLMException',原因 : 'Migration is required due to the following errors

ios - 当我单击TableView单元格时, TableView 崩溃

ios - 如何正确覆盖 reloadRowsAtIndexPaths :withRowAnimation?

objective-c - 自定义单元格中具有备用背景颜色的 UITableViewCell

ios - 隐藏在滚动/滑动后,UINavigationBar 不会再次显示

swift - 阿拉莫菲尔 4 请求开放天气

swift - 如何在 Xcode (swift) 中使文本自动滚动到右侧?

c++ - 将旧的 Mecab 库实现到现代 iOS 应用程序中