ios - UITableView 部分删除问题

标签 ios swift uitableview

我有 2 个部分1/59 行。 我删除了行(所以在 0 节中我得到了 0 行),并遇到了这个崩溃(也在 -[UITableView deleteRowsAtIndexPaths:withRowAnimation:] 工作的断点)。无法得到这个 1 deleted) 是如何计算的。

Invalid update: invalid number of sections.  The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (2), plus or minus the number of sections inserted or deleted (0 inserted, 1 deleted)

我收到此消息,但根本没有删除部分,-[UITableView deleteSections:withRowAnimation:] 处的断点不起作用。

有什么想法/建议吗?

谢谢!

最佳答案

我想发生了什么,但我不确定,因为我没有看到您的代码。 我将尝试向您解释当您要删除表中的行或部分时应遵循的过程。

我假设您有一个包含行的数组和另一个包含部分的数组。

1 - 您必须从行数组中删除一行:

arrayRows.removeAtIndex(indexRow)

2 - 您必须从部分数组中删除一个部分(如果需要):

arraySections.removeAtIntex(indexSection)

3 - 现在是从表中删除行的时刻,使用下一个代码:

tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths(indexRow, withRowAnimation: UITableViewRowAnimation.Fade)
//If you want delete sections, firstly you must delete all rows of this section
tableView.deleteSections(indexSection, withRowAnimation: UITableViewRowAnimation.Fade)
tableView.endUpdates()

//After this, you must reload data of table
tableView.reloadData()

PD:确保您的方法 numberOfRowsInSection 和 numberOfSectionsInTableView 从行和节的数组中获取数据。像这样:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return arraySections.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //Return array of rows of section
    return arrayRows.count
}

希望对您有所帮助!

如果您有任何疑问,请告诉我,我会尝试以其他方式帮助您。

关于ios - UITableView 部分删除问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38741302/

相关文章:

swift - 如何让 task2 在 task1 快速完成后执行?使用 dispatch_sync?

ios - 为什么 return 0 永远不会在这段代码中执行?

ios - Xamarin UITableVIew-扩展并折叠一行

ios - UITextField clearButton 在出现时调整文本字段的插入

ios - 将参数传递给自定义 UITableViewCell

ios - 表格 View 在滚动时返回错误的值

ios - 如何在 iOS SDK 中使用离线语音识别?

ios - 当我在 block 中使用 self 时,我得到一个 nil。但是 self 没有释放

ios uiscrollview delayscontenttouches 仅适用于垂直滑动

ios - 发送到实例的无法识别的选择器