Ios 当我尝试折叠UITableView节时,索引超出范围
根据jeantimex关于如何展开/折叠节和HI的回答,我添加了以下代码,以在点击节时隐藏行:Ios 当我尝试折叠UITableView节时,索引超出范围,ios,swift,uitableview,Ios,Swift,Uitableview,根据jeantimex关于如何展开/折叠节和HI的回答,我添加了以下代码,以在点击节时隐藏行: struct Person { //this is my data let name: String var item: [(itemName: String, price: Decimal)] var collapsed: Bool! init(name: String, item: [(itemName: String, price: Decimal)
struct Person {
//this is my data
let name: String
var item: [(itemName: String, price: Decimal)]
var collapsed: Bool!
init(name: String, item: [(itemName: String, price: Decimal)], collapsed: Bool = false) {
self.name = name
self.item = item
self.collapsed = collapsed
}
}
class TableSectionHeader : UITableViewHeaderFooterView {
//this is my custom header section
var delegate: CollapsibleTableViewHeaderDelegate?
var section: Int = 0
@IBOutlet weak var lblPerson: UILabel!
@IBOutlet weak var lblTotal: UILabel!
func tapHeader(_ gestureRecognizer: UITapGestureRecognizer) {
guard let cell = gestureRecognizer.view as? TableSectionHeader else {
return
}
delegate?.toggleSection(self, section: cell.section)
print(cell.section)
}
}
protocol CollapsibleTableViewHeaderDelegate {
func toggleSection(_ header: TableSectionHeader, section: Int)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return personArray[indexPath.section].collapsed! ? 0 : 44.0
}
在我的视图forheaderin部分
委托中,我添加了一个UITapGestureRecognitor
:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = billTableView.dequeueReusableHeaderFooterView(withIdentifier: "TableSectionHeader")
let header = cell as! TableSectionHeader
header.section = section
header.delegate = self
header.lblPerson.text = structArray[section].name
header.lblTotal.text = SplitBill().displayIndividualTotal(person: structArray, section: section)
header.addGestureRecognizer(UITapGestureRecognizer(target: header.self, action: #selector(header.tapHeader(_:))))
return cell
}
分区可以完美地折叠/展开,但是我有一个按钮可以删除分区,当我删除第一个分区(0)并尝试点击另一个分区时,应用程序崩溃,出现错误:
致命错误:索引超出范围
我做了一些调试来打印节索引,并意识到当我从数据中删除一个对象时,toggleSection
函数仍然保存着第二节的索引(1):
您正在重用标题视图,但没有更新
部分
,以与新的数据源结构相对应。您需要更新折叠/展开后可见的页眉视图。我注意到您从页眉单元格中获取该节。
-是否因为页眉被重用,所以在折叠时您没有更新该节?@sooper既然您提到了它,我想可能是这样的。当我删除第一个对象时,单元格.section
没有更新它的索引..在删除该节之后,我在我的tableView中添加了一个reloadData()
,现在它可以工作了…这是另一种方式,尽管我不确定在折叠时你是否仍然可以得到你想要的动画?嗯,是的,当我删除第一部分时,动画似乎有一个问题。。。第二部分只向上移动,而不是同时向上移动部分和行。这样认为-您需要遍历可见单元格并重新配置它们以保留动画。我怎么做?我编辑了我的问题以包含我的删除部分代码。
extension BillForm: CollapsibleTableViewHeaderDelegate {
func toggleSection(_ header: TableSectionHeader, section: Int) {
print(section) //index is 1 although I have removed the first object
let collapsed = !personArray[section].collapsed
// Toggle collapse
personArray[section].collapsed = collapsed
// Adjust the height of the rows inside the section
billTableView.beginUpdates()
for i in 0 ..< personArray[section].item.count {
billTableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
}
billTableView.endUpdates()
}
}
for i in (0..<self.personArray[row].item.count).reversed() {
let rowIndex = IndexPath(row: i, section: row)
self.personArray[row].item.remove(at: i) //remove rows first
self.billTableView.deleteRows(at: [rowIndex], with: .right)
}
self.personArray.remove(at: row) //then remove section
self.billTableView.deleteSections(IndexSet(integer: row), with: .right)
self.billTableView.reloadData()