【问题标题】:Refresh or reload data in function ViewWillAppear not always working in UITableViewcontroller在函数 ViewWillAppear 中刷新或重新加载数据并不总是在 UITableViewcontroller 中工作
【发布时间】:2019-11-09 16:10:07
【问题描述】:

我将一个 UITabbarViewController 设置为我的应用程序的第一个视图,而 UITableViewcontroller 是一个选项卡,这个表视图将显示一些从 URL 响应数据中获取的事务数据。

我所做的是我定义了一个数组作为tableviewcontroller的成员,然后在Viewwillappear中数组数据每次都会刷新,预期的结果是:每当我点击tableview选项卡时,数据都会重新加载或再次刷新。

现在的问题是有时它工作得很好,但有时它显示空白表格视图

我在 stackoverflow 中搜索了类似的问题,发现了类似的问题,但提供的解决方案对我不起作用。例如下面

Swift: How to reload new content (eg. array) every time the tableview appears?

下面是我的代码:

class TransactionTableViewController: UITableViewController {

var transactionList = [transactionDetail]()

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.dataSource=self
    self.tableView.delegate=self
}


 override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    //every tap, trigger reload
    reloadTransaction()
    //refresh table view
    self.tableView.reloadData()
}


internal func reloadTransaction(){
    self.transactionList.removeAll()

    //get URL response and append data to transactinList
    ....
}


override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return transactionList.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    //if transactionList.count>0 {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TransactionTableViewCell", for: indexPath) as!
        TransactionTableViewCell

    // Configure the cell...

    cell.scanTime.text=transactionList[indexPath.row].scanTime
    cell.productName.text=transactionList[indexPath.row].productName
    cell.productID.text=transactionList[indexPath.row].productID
    cell.location.text=transactionList[indexPath.row].location

    return cell
    //}
}

有什么我想念的吗?我希望数据应该始终正确

【问题讨论】:

  • 您必须在“reloadTransaction()”方法中重新加载表格视图,而不是在 viewWillAppear 方法中。

标签: ios swift uitableview


【解决方案1】:

您应该考虑两件事。如果您的 url 请求尚未处理,则在 reloadTransaction() 之后将立即调用第一次重新加载数据,它将显示一个空的 tableView。

其次,当您有新数据时,请确保在调用 self.tableView.reloadData() 之前调用主线程:

internal func reloadTransaction(){
    self.transactionList.removeAll()

    //get URL response and append data to transactinList
    ....
    //call self.tableView.reloadData() here after you append the new data

    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
}

【讨论】:

  • 谢谢!当我将 tableview.reloaddata 放在 reloadtransaction 函数中时,它工作得很好!另外问题是,似乎我仍然将它 reloaddata 放入主线程,是否有一些最佳实践或苹果文档?
  • 不客气。 UI 更新/更改应始终在主线程中完成。因为重载数据,重绘tableview和单元格。它应该在主线程上调用。
猜你喜欢
  • 1970-01-01
  • 2012-02-07
  • 1970-01-01
  • 2012-07-31
  • 2014-02-19
  • 2019-05-28
  • 2011-11-29
  • 1970-01-01
  • 2019-09-19
相关资源
最近更新 更多