【发布时间】: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