ios - 如何制作可展开的侧边菜单 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
}
}
我该如何解决这个问题。
解决方案
推荐阅读
- ruby-on-rails - 如何以最佳方式将纱线包与 Rails 6 集成?
- java - Hibernate JPA 集成测试问题
- python - 从 Python 对象调用方法
- hyperlink - 将 Jekyll 页面 (Markdown) 拆分为可链接的部分
- javascript - GSAP On 滚动视频和文本淡入淡出动画
- hash - 为什么哈希函数很快?
- google-chrome - chrome 主题也可以覆盖新标签的行为吗?
- c# - 通过 ClickOnce 发布 WPF 应用程序时,引用项目的 .NET 5 TargetFramework 问题
- r - 通过excel文件R重命名数据框中的列
- javascript - 作用域如何在 for 循环中工作?让和未定义