问题
Im trying to expand my tableView, but Im getting an
Thread 1: signal SIGABRT error. Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSSingleObjectArrayI objectAtIndex:]: index 6 beyond bounds [0 .. 0]'
This is the code that I wrote.
override func viewDidLoad() {
super.viewDidLoad()
tableViewData = [cellData(opened: false, title: "Name", sectionData: ["Cell 1", "Cell 2", "Cell 3"]),
cellData(opened: false, title: "Type", sectionData: ["Cell 1", "Cell 2", "Cell 3"]),
cellData(opened: false, title: "Color", sectionData: ["Cell 1", "Cell 2", "Cell 3"]),
cellData(opened: false, title: "Plate", sectionData: ["Cell 1", "Cell 2", "Cell 3"]),
cellData(opened: false, title: "Setting", sectionData: ["Cell 1", "Cell 2", "Cell 3"]),
cellData(opened: false, title: "About", sectionData: ["Cell 1", "Cell 2", "Cell 3"]),
cellData(opened: false, title: "Logout", sectionData: ["Cell 1", "Cell 2", "Cell 3"])]
}
override func numberOfSections(in tableView: UITableView) -> Int {
return tableViewData.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableViewData[section].opened == true {
return tableViewData[section].sectionData.count
} else {
return 1
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else { return UITableViewCell()}
cell.textLabel?.text = tableViewData[indexPath.section].title
return cell
} else {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else { return UITableViewCell()}
cell.textLabel?.text = tableViewData[indexPath.section].sectionData[indexPath.row]
return cell
}
}
How do I solve this problem.
来源:https://stackoverflow.com/questions/57799390/how-to-make-a-expandable-side-menu-tableview-list