0

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
        }
    }

enter image description here

How do I solve this problem.

Nick
  • 875
  • 6
  • 20
Nick
  • 104
  • 11
  • 1
    The content of the table view is **static**. Is this intended? The code is clearly related to *Dynamic Prototype*. And please name structs and classes with starting capital letter und force unwrap the table view cells. If the code crashes it reveals a **design** mistake. – vadian Sep 05 '19 at 09:30
  • i think its a design mistake. because when i try to a normal size tableViewController, it works. but when its like this, its giving me an error. the tutorial for the side menu and the tutorial for the expandable tableview are from 2 different people. – Nick Sep 05 '19 at 09:48
  • 1
    No, I mean the *design mistake* is revealed if the cells are force unwrapped (rather than *guard*ed) in the `dequeueReusableCell` lines and the code crashes in those lines. – vadian Sep 05 '19 at 09:50
  • i made it into Dynamic, its somehow working. – Nick Sep 05 '19 at 09:50
  • what if i dont want everything to expand, for example i only want Settings to expand and how do i call out didSelectRow – Nick Sep 05 '19 at 10:41
  • This is another question and depends on the design how the cells are expanded/collapsed – vadian Sep 05 '19 at 10:43

0 Answers0