How to make a expandable side menu tableView list

人走茶凉 提交于 2020-01-06 05:09:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!