когда приложение запускает этот
if section == 0 && sectionArray[0].isExpanded == false
, поэтому число строк равно 0 в соответствии с ectionArray[0].section.count - 1
, затем, когда вы щелкаете действие handleExpandClose, остальное запускается
} else {
sectionArray[0].section.append("")
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .fade)
в нем вы добавляете данные к внутреннему массиву внутри единственного объекта, поэтому при вставке раздел основного массива dataSource не изменяется, поэтому происходит сбой
class TableViewController: UITableViewController {
var sectionArray = [ExpandableCell(),ExpandableCell(),ExpandableCell()]
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
// simulate collapse action
DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
self.sectionArray[0].isExpanded = false
self.tableView.reloadData()
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return sectionArray.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return sectionArray[section].isExpanded ? sectionArray[section].content.count : 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
// Configure the cell...
cell.textLabel?.text = sectionArray[indexPath.section].content[indexPath.row]
return cell
}
}
struct ExpandableCell {
var isExpanded = true
var content = ["1","2","3"]
}