tableView.reloadData() 表未更新 [英] tableView.reloadData() Table not updating

查看:24
本文介绍了tableView.reloadData() 表未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表格不显示更新后的数组返回单元格.当我启动应用程序时,我得到空白单元格.故事板中已授予所有权限.我在任何地方都尝试了 tableView.reloadData() 但似乎也无法让它工作.如果有人能解释我哪里出错了,那真的会帮助我变得更好.

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

推荐答案

通过在 cellForRowAt 内调用 tableView.reloadData(),你创建了一个无限循环,因为 reloadData() 自动调用 cellForRowAt.您需要将 reloadData() 移动到 tableGenerate() 中,因为它应该只在您的数据源更改时调用.

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.

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

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

这篇关于tableView.reloadData() 表未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆