该表不显示更新的数组以返回到单元格。当我启动应用程序时,我得到空白单元格。所有权限都在故事板中给出。我尝试了tableView.reloadData()
无处不在,但似乎无法使其工作。如果任何人都可以解释我哪里错了,那真的会帮助我好起来。tableView.reloadData()表未更新
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var slider: UISlider!
@IBAction func sliderSelector(_ sender: Any) {
tableGenerate()
}
var arrayTable = [Int]()
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayTable.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "Cell")
cell.textLabel?.text = String(arrayTable[indexPath.row])
tableView.reloadData()
return cell
}
func tableGenerate() {
var tableMultiplier = 1
while tableMultiplier <= 50 {
arrayTable.append(tableMultiplier * Int(slider.value))
tableMultiplier += 1
print(arrayTable)
}
}
为什么要在'cellForRowAt'内调用'tableView.reloadData()'?这会让你进入无限循环,因为它会再次调用'cellForRowAt' ...如果在设置了表视图后更改数据源,则只需调用'tableView.reloadData()'。 –
数据在滑块移动时得到更新。错了? – Dev0urCode
是的,所以你应该从'sliderSelector'内调用'tableView.reloadData()',或者在'tableGenerate()'... @ Dev0urCode检查我的答案。 –