【问题标题】:Fatal error: Index out of range in TableView inside ViewController致命错误:ViewController 内 TableView 中的索引超出范围
【发布时间】:2019-11-16 09:23:02
【问题描述】:

基本上我在 ViewController 中有一个名为 TransferTableView 的 TableView,我的目的是从一个名为 fetchJSON() 的函数中加载数据,该函数用行和部分填充 tableView 以进行组织。

问题是我不断收到错误,

致命错误:ViewController 内 TableView 中的索引超出范围。

对于numberOfRowsInSection 中的let section = sections[section] 行,我不明白是什么原因导致出现此错误。

【问题讨论】:

  • 你打印了sections数组吗?在您访问section[section]之前它可能是空的

标签: ios swift uitableview uiviewcontroller


【解决方案1】:

在你的数据源中添加这个函数

func numberOfSections(in tableView: UITableView) -> Int {
  return sections.count
}

【讨论】:

    【解决方案2】:

    当表格在加载数据之前出现时,数组为空,numberOfSections 的默认值为 1

    let section = sections[section] // = sections[0]
    

    因此崩溃,您可以通过添加来修复它

    func numberOfSections(in tableView: UITableView) -> Int {
      return sections.count
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 2016-08-15
      • 1970-01-01
      相关资源
      最近更新 更多