ios - Swift - 在 Controller 之间移动后表为空

标签 ios swift uitableview

我正在更新现有的 Objective-C 应用。

有一个结构: 应用程序代理 - 创建 mainBackgroundView 并使用 UITabBarController 添加 subview

我有一个“选项卡”HistoryViewController:

@objc class HistoryViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    let historyTableViewController = HistoryTableViewController()        
    self.view.addSubview(historyTableViewController.view)
  }

}

和HistoryTableViewController:

import UIKit

@objc class HistoryTableViewController: UITableViewController {

// Mark: properties

var historyCalls = [HistoryItem]()

// Mark: private methods
private func loadSimpleHistory() {
    let hist1 = HistoryItem(personName: "Test", bottomLine: "text", date: "10:47")
    let hist2 = HistoryItem(personName: "Test 2", bottomLine: "text", date: "10:47")
    let hist3 = HistoryItem(personName: "Test 3", bottomLine: "text", date: "10:47")
            historyCalls += [hist1, hist2, hist3]
}




override func viewDidLoad() {
    super.viewDidLoad()

    self.loadSimpleHistory()

    self.tableView.register(UINib(nibName: "HistoryCallTableViewCell", bundle: nil), forCellReuseIdentifier: "HistoryCallTableViewCell")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

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 historyCalls.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "HistoryCallTableViewCell", for: indexPath) as? HistoryCallTableViewCell else {
        fatalError("Coulnd't parse table cell!")
    }

    let historyItem = historyCalls[indexPath.row]

    cell.personName.text = historyItem.personName
    cell.bottomLine.text = historyItem.bottomLine
    cell.date.text = historyItem.date


    return cell
}

}

当我第一次使用 HistoryViewController 打开导航选项卡时,表格中会显示数据。当我点击表格或切换 navTab 然后返回时,表格不再存在。

当我切换到另一个应用程序然后返回时,表格又出现了。

如何解决这个问题?

谢谢

最佳答案

在viewwillappear中调用数据方法并重新加载表格..

  override func viewWillAppear() {
     super.viewWillAppear()
     self.loadSimpleHistory()
     self.tableview.reloadData()
  }

关于ios - Swift - 在 Controller 之间移动后表为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42886789/

相关文章:

ios - 查看 EkReminder 重复频率的最佳方式?

ios - 消息 ' do not match any available overloads' 是什么意思?

iphone - UITableView 滚动缓慢,内存泄漏问题

ios - 单击 Pin 以转到另一个 ViewController

ios - map 无法显示我的位置

ios - CALayer 蒙版周围的边框

ios - 如何使用 Swift 修复 iOS 应用程序上的 fatal error ?

ios - UItableView 用 UIButton 替换复选标记

ios - uitableview重置删除按钮

ios - 当表格 View 太大而无法显示在屏幕上时,打印表格 View 的所有行