简体   繁体   English

tableView.reloadData()表未更新

[英]tableView.reloadData() Table not updating

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. 我在任何地方都尝试过tableView.reloadData() ,但似乎也无法使其正常工作。 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)
    }
}

Add outlet of tableview like this and connect with table in storyboard- 像这样添加tableview的出口,并在情节提要中连接表-

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

}

By calling tableView.reloadData() inside cellForRowAt , you create an infinite loop, since reloadData() automatically calls cellForRowAt . 通过在cellForRowAt内部调用tableView.reloadData() ,您可以创建一个无限循环,因为reloadData()自动调用cellForRowAt You need to move reloadData() inside tableGenerate() , since it should only be called when your data source is changed. 您需要在tableGenerate()移动reloadData() ,因为仅当更改数据源时才应调用它。

You also need to create an IBOutlet for your tableView in Storyboard . 您还需要在Storyboard为tableView创建IBOutlet。 Since you are not using a UITableViewController , you need to do this manually. 由于您没有使用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()
    }
}

forgot to create the table outlet ! 忘记创建桌子插座! I also forgot to reset the array with the array slider with arrayTable = [] now it works fine 我也忘记了使用arrayTable = []的数组滑块重置数组,现在它可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM