致命错误:索引超出范围swift
问题描述:
我在UITableView中遇到索引超出范围问题。 下面是我的代码:致命错误:索引超出范围swift
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
//== Teble: number of tables ==============================//
func numberOfSections(in tableView: UITableView) -> Int {
return headerTitleArray.count
}
//== Teble rows: number of rows ==============================//
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellTitleMultiArray.count
}
//== Teble rows: data for each row ==============================//
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomTableViewCell
let text = cellTitleMultiArray[indexPath.section][indexPath.row]
cell.titleLabel.text = text
return cell
}
}
我试图解决这个问题,但不能。请请帮我解决这个问题....
答
它看起来像你的cellTitleMultiArray
是一个数组的数组,其中外部数组包含每个部分的条目,并且对于每个部分,该索引处的内部数组包含该部分中每行的条目。
因此numberOfSections(in:)
应该可能返回cellTitleMultiArray
中的条目数。
tableView(_:numberOfRowsInSection:)
应该可能返回当前部分的cellTitleMultiArray
子数组中的条目数。看看你是否可以制定出这样的代码。
+0
我有一个viewForHeaderInSection:我应该在哪里调用数组的返回值? –
这是正确的'return cellTitleMultiArray.count'?听起来像你想要的。我认为它应该是'return cellTitleMultiArray [section] .count' –
'return cellTitleMultiArray.count' =>'return cellTitleMultiArray [indexPath.section] .count' – Larme
我试过了cellTitleMultiArray [section] .count,但它崩溃了 –