UITableView部分删除问题

时间:2016-08-03 10:42:21

标签: ios swift uitableview

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

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 个答案:

答案 0 :(得分:-1)

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

我认为你有一个包含行的数组和包含各个部分的其他数组。

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
}

我希望它有所帮助!!

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

相关问题