swift - 删除最后一行并同时加载多行

标签 swift uitableview

当我从表格 View 中删除最后一行时:

tableView.deleteRows(at: [indexPath], with: .middle)

并将我的表的数据源更改为另一个数组:

if orders.isEmpty {
    return ABCDArray.count
} else {
    return orders.count
}

它给了我这个错误:

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 (20) must be equal to the number of rows contained in that section before the update (1), 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).'

我该如何解决这个问题?更改数据源有什么问题?

最佳答案

在调用 tableView.deleteRows(at: [indexPath], with: .middle) 之前,您需要从数据数组中删除该对象。因此,您的代码应如下所示:

// Editing of rows is enabled
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        //when delete is tapped
        orders.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .middle)
    }
}

关于swift - 删除最后一行并同时加载多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48123727/

相关文章:

ios - swiftyJSON 从数组中获取随机索引

ios - 从 Swift 函数中的异步调用返回数据

swift - 更新后的核心数据模型属性无法访问?

ios - 带有 SubView 的 tableView 的容器 View 或 UIViewController

ios - 如何等到 UITableView 内置动画完成?

ios - 如何为自动调整单元格正确设置自动布局约束

ios - 如何从 json 调用评级 View 并在我的 TableView 中显示评级

ios - 广告关闭后状态栏背景发生变化

arrays - TableView 数组索引中的第一个单元格超出范围错误

Xcode:使用 Swift 将默认自定义字体添加到 SKLabelNode