简体   繁体   English

更新 TableView 单元格数据

[英]Update TableView cell data

I'm new to Swift/Xcode and reached a problem with updating TableView cell data.我是 Swift/Xcode 的新手,在更新 TableView 单元格数据时遇到了问题。 I can't quite seem to find/implement the answer to this simple thing from any similar StackOverflow questions either.我似乎也无法从任何类似的 StackOverflow 问题中找到/实现这个简单问题的答案。

I have an app that starts making an api call every 1 sec on a timer once a button is pressed but the problem is that when all tableview data gets loaded from an array with viewDidLoad() theres obviously no data to display in the label as no call has been made yet.我有一个应用程序,一旦按下按钮,就会开始在计时器上每隔 1 秒调用一次 api,但问题是当所有 tableview 数据从带有viewDidLoad()的数组中加载时,显然没有数据可以在 label 中显示为 no电话已经打了。

Once I press the button the data starts flowing but nothing appears in the label.一旦我按下按钮,数据开始流动,但 label 中没有出现任何内容。 I believe this is because the label needs to be updated upon every call in order to display the new data.我相信这是因为 label 需要在每次调用时更新以显示新数据。

The data is also an NSAttributedString from HTML.数据也是来自 HTML 的 NSAttributedString。 Not sure if this is relevant though as it seems to work fine in test cell's outside of the tableview.不确定这是否相关,因为它似乎在表格视图之外的测试单元格中工作正常。

Essentially what I want to do is somehow call基本上我想要做的是以某种方式调用

    cell.cellLabelTwo.attributedText = tableData[indexPath.row].secondRowLabel

from the code shown below every time a new api call is made and update cellLabelTwo with the new % gain data.每次调用新的 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

}

Edit: I have added @IBOutlet var tableView: UITableView!编辑:我添加了@IBOutlet var tableView: UITableView! and connected it to View Controller, I've also assigned the tableview to datasource and delegate of View Controller.并将其连接到 View Controller,我还将 tableview 分配给 View Controller 的数据源和委托。 Adding the IB outlet seemed to be solution to this for many other people but it still not showing anything in the cell label...对于许多其他人来说,添加 IB 插座似乎是解决这个问题的方法,但它仍然没有在单元格 label 中显示任何内容......

You simply need to reload the table view after each API call.您只需在每次 API 调用后重新加载表视图。

tableView.reloadData()

This would be best called in a completion handler of your API call, but you don't provide your code where the API is called.这最好在您的 API 调用的完成处理程序中调用,但您不提供调用 API 的代码。

Reloading the data will call the delegate method cellForRowAt: for each cell, which is where you have the code that you want to run.重新加载数据将为每个单元格调用委托方法cellForRowAt:这是您拥有要运行的代码的地方。

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

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