У меня проблема с источником данных. причина: «попытка вставить строку 0 в раздел 0, но после обновления в разделе 0 осталось только 0 строк».
Я пытался развернуть и свернуть раздел 1 моего табличного представления. Когда мне впервые представили контроллер представления, я могу развернуть его, а затем свернуть, но когда я пытаюсь развернуть его во второй раз, он вылетает. Я пытаюсь добавить + 1, когда он расширяется в numberOfRows, но это тоже дает сбой. не знаю, что я делаю неправильно и что мне нужно добавить, чтобы это сработало.
Изменить * Когда я сначала нажимаю, чтобы развернуть раздел, внутри numberofRowsInSection запускается оператор if isExpanded == false, который дает мне section.count - 1. Но почему это выполняется и возвращает мне строку? Кажется, моя проблема как-то связана с этим, но IDK исправляет.
var sectionArray = [ ExpandableCell(isExpanded: false, section: [""])
]
@objc func handleExpandClose(button: UIButton) {
let indexPath = IndexPath(row: 0, section: 0)
let isExpanded = sectionArray[0].isExpanded
if isExpanded {
sectionArray[0].section.removeAll()
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.endUpdates()
} else {
sectionArray[0].section.append("")
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .fade)
tableView.endUpdates()
}
sectionArray[0].isExpanded.toggle()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 && sectionArray[0].isExpanded {
return sectionArray[0].section.count
} else if section == 0 && sectionArray[0].isExpanded == false {
return sectionArray[0].section.count - 1
}
else if section == 1 {
return 1
}
return 0
}