我正在
致命错误:指数超出范围"和"线程1:EXC_BAD_INSTRUCTION (code = EXC_I386_INVOP,subcode = 0x0)" at line" 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
}
}
答案 0 :(得分:3)
尝试进行此类更改
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayTable.count
}
func tableGenerate () {
var tableMultiplier = 1
while tableMultiplier <= 50 {
arrayTable.append(tableMultiplier * Int(slider.value))
tableMultiplier += 1
}
DispatchQueue.main.async {
print(arrayTable)
tblview.reloadData()
}
}
答案 1 :(得分:2)
新手的常见错误是从numberOfRowsInSection
返回任何幻数。而不是你应该返回你的真实物品数量return arrayTable.count
,我相信一切都应该正常工作。
答案 2 :(得分:0)
当视图加载时,tableView将尝试返回50个单元格。对于您尝试将textlabel文本分配到的每个单元格:
cell.textLabel?.text = String(arrayTable[indexPath.row])
但是因为在加载视图时arrayTable为空,所以当应用程序试图通过indexPath.row访问索引时,应用程序会崩溃。
答案 3 :(得分:0)
此方法存在问题
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return yourArray.count
}