ios - 当我通过按钮操作从 TableView 中删除单元格时,应用程序崩溃了 - iOS Swift

标签 ios swift tableview

我正在尝试从 TableViewController 中删除一个单元格。每次我滑动并按下删除按钮时都会发生崩溃。

这是崩溃日志:

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 (10) must be equal to the number of rows contained in that section before the update (10), 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 函数。

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as! PredefinedServicesCell

    //Cell configuration.
    myCell.selectionStyle = .None
    myCell.containerView.layer.borderColor = UIColor(hex: 0x3399CC).CGColor
    myCell.containerView.layer.borderWidth = 1
    myCell.containerView.layer.cornerRadius = 10
    myCell.containerView.clipsToBounds = true
    myCell.servicePrice.text = "\(indexPath.row)"
    myCell.serviceCurrency.text = "KWD"
    myCell.serviceTitle.text = "Building iOS Application"
    myCell.serviceDescription.text = "This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description This is a service description"
    return myCell
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    if editingStyle == .Delete {
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    }

}

可能是什么原因?

最佳答案

请动态添加您的numberOfRowsInSection。它不是静态的。

使用return 10.插入的数组,并在编辑样式中删除时删除objec。

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return yourArray.count
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    if editingStyle == .Delete {
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

// remove object from array here

    }

}

错误背后的原因:在您的情况下

首先您的表格单元格数量为 10。 当您删除单元格时,您的单元格将被删除,但计数为 10。它已磨损,否则将计数减少到 9。

关于ios - 当我通过按钮操作从 TableView 中删除单元格时,应用程序崩溃了 - iOS Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38280997/

相关文章:

swift - TableView 在新 Controller 上显示不同的图像

ios - 动画 UITableViewCell 以编程方式移动到底部

ios - 如何为自定义表格 View 单元动态设置约束?

ios - 未经身份验证的身份未映射到开发人员身份验证的身份

ios - 如何在iOS 10中删除贴纸包

ios - objc_getAssociatedObject() 始终为零

swift - 基于类型限制 APNS,Swift 3

ios - Google SDK iOS - sign() 方法完成处理程序

ios - 在 ios8 中使用 swift 语言发送短信

ios - 将界面生成器中的 tableView 连接到 NON-outlet 变量