Fatal error: Index out of range in TableView inside ViewController

Fatal error: Index out of range in TableView inside ViewController

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

问题是我一直收到错误,

Fatal error: Index out of range in TableView inside ViewController.

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

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

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

因此崩溃,您可以通过添加

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

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

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