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() ,你创build一个无限循环,因为reloadData()自动调用cellForRowAt 。 您需要在tableGenerate()移动reloadData() ,因为只能在数据源更改时调用它。

您还需要在Storyboard为您的tableView创build一个IBOutlet。 由于您没有使用UITableViewController ,因此您需要手动执行此操作。

 class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var tableView: UITableView! @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]) return cell } func tableGenerate () { var tableMultiplier = 1 while tableMultiplier <= 50 { arrayTable.append(tableMultiplier * Int(slider.value)) tableMultiplier += 1 print(arrayTable) } tableView.reloadData() } } 

像这样添加tableview的出口,并在storyboard-

 @IBOutlet weak var tableView1: UITableView! 

将代码更改为此 –

 class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var slider: UISlider! @IBOutlet weak var tableView1: UITableView! @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]) return cell } func tableGenerate () { var tableMultiplier = 1 while tableMultiplier <= 50 { arrayTable.append(tableMultiplier * Int(slider.value)) tableMultiplier += 1 } print(arrayTable) tableView1.reloadData() } 

忘了创build表格sockets! 我也忘了用数组滑块arrayTable = []重置数组,现在它工作正常