更新TableView单元格数据

我是Swift / Xcode的新手,在更新TableView单元格数据时遇到问题。我似乎也无法从任何类似的StackOverflow问题中找到/实现这一简单问题的答案。

我有一个应用程序,一旦按下按钮,该应用程序每隔1秒就会开始在计时器上进行api调用,但问题是,当所有的tableview数据从带有viewDidLoad()的数组加载时,显然没有数据可显示标签中,因为尚未拨打电话。

一旦我按下按钮,数据便开始流动,但标签中什么也没有出现。我相信这是因为标签需要在每次通话时进行更新才能显示新数据。

数据也是HTML中的NSAttributedString。不确定这是否相关,因为它似乎可以在表格视图外部的测试单元中正常工作。

基本上我想做的就是打电话

    cell.cellLabelTwo.attributedText = tableData[indexPath.row].secondRowLabel
每次进行新的api调用时,从下面显示的代码中

并使用新的%增益数据更新cellLabelTwo。

struct MyData {
    var imageRow: UIImage
    var firstRowLabel: String
    var secondRowLabel: NSAttributedString
}

var tableData: [MyData] = []

override func viewDidLoad() {
    super.viewDidLoad()

    tableData = [
        MyData(imageRow: UIImage(named: "ada.png")!,firstRowLabel: "ADA/USDT",secondRowLabel: adaPercentGainString.htmlToAttributedString!)
    ]

}


func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
    return tableData.count
}


func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "protoCellOne") as! TableViewCell

    cell.cellImageone.image = tableData[indexPath.row].imageRow
    cell.cellLabelOne.text = tableData[indexPath.row].firstRowLabel
    cell.cellLabelTwo.attributedText = tableData[indexPath.row].secondRowLabel
    cell.cellLabelTwo.font = UIFont(name: "Helvetica Neue",size: 19)
    cell.cellLabelTwo.textAlignment = .right

    return cell

}

编辑:我添加了@IBOutlet var tableView: UITableView!并将其连接到View Controller,我还将表视图分配给了数据源和View Controller的委托。对于其他许多人来说,添加IB插座似乎是解决此问题的方法,但它仍未在单元标签中显示任何内容...

hhab12345678 回答:更新TableView单元格数据

您只需要在每次API调用后重新加载表视图即可。

tableView.reloadData()

最好在API调用的完成处理程序中调用此方法,但不要在调用API的地方提供代码。

重新加载数据将为每个单元格调用委托方法cellForRowAt:,这是您要运行代码的位置。

本文链接:https://www.f2er.com/3132903.html

大家都在问