簡體   English   中英

Swift-在控制器之間移動后表為空

[英]Swift - table is empty after moving between controllers

我正在更新現有的Objective-C應用程序。

有一個結構:AppDelegate-創建mainBackgroundView並使用UITabBarController添加子視圖

我有一個“標簽” 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並返回時,表不​​再存在。

當我切換到另一個應用程序然后返回時,表又出現了。

如何解決這個問題?

謝謝

出現在view中的調用數據方法並重新加載表。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM