【发布时间】:2017-08-17 10:29:50
【问题描述】:
我来了
致命错误:索引超出范围”和“线程 1:EXC_BAD_INSTRUCTION (代码=EXC_I386_INVOP,子代码=0x0)”在“cell.textLabel?.text = 字符串(arrayTable[indexPath.row])
当我在操场上测试我的功能时,它按预期工作。我的数组被填充。所以,我不明白为什么 Xcode 在我的数组中找不到任何值。
有什么想法吗?
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var slider: UISlider!
@IBAction func sliderSelector(_ sender: Any) {
arrayTable = [Int]()
tableGenerate()
}
var arrayTable = [Int]()
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "Cell")
cell.textLabel?.text = String(arrayTable[indexPath.row])
return cell
}
func tableGenerate () {
var tableMultiplier = 1
while tableMultiplier <= 50 {
arrayTable.append(tableMultiplier * Int(slider.value))
tableMultiplier += 1
}
}
【问题讨论】:
-
fatal error: Index out of range表示numberOfRowsInSection不与cellForRowAtIndexPath与您的数组计数同步。 -
如下所示,将 return 50 更改为 return arrayTable.count 修复了崩溃
标签: arrays swift uitableview swift3