該表不顯示更新的數組以返回到單元格。當我啓動應用程序時,我得到空白單元格。所有權限都在故事板中給出。我嘗試了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檢查我的答案。 –