首页 > 解决方案 > 如何制作可展开的侧边菜单 tableView 列表

问题描述

我试图扩展我的 tableView,但我得到了一个

线程 1:信号 SIGABRT 错误。由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSSingleObjectArrayI objectAtIndex:]: index 6 beyond bounds [0 .. 0]”

这是我写的代码。

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

在此处输入图像描述

我该如何解决这个问题。

标签: iosswiftuitableviewexpandablelistviewside-menu

解决方案


推荐阅读