tableView.reloadData() Table not updating

左心房为你撑大大i 提交于 2019-12-12 17:30:00

问题


The table does not display the updated array to return to the cell. When I launch the app I get blank cells. All permissions have been given in the storyboard. I tried the tableView.reloadData() everywhere but can't seem to make it work either. If anyone can explain where Im going wrong that would really help me get better.

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)
    }
}

回答1:


By calling tableView.reloadData() inside cellForRowAt, you create an infinite loop, since reloadData() automatically calls cellForRowAt. You need to move reloadData() inside tableGenerate(), since it should only be called when your data source is changed.

You also need to create an IBOutlet for your tableView in Storyboard. Since you are not using a UITableViewController, you need to do this manually.

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()
    }
}



回答2:


Add outlet of tableview like this and connect with table in storyboard-

@IBOutlet weak var tableView1: UITableView!

Change code to this -

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()

}



回答3:


forgot to create the table outlet ! I also forgot to reset the array with the array slider with arrayTable = [] now it works fine



来源:https://stackoverflow.com/questions/45736553/tableview-reloaddata-table-not-updating

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!