解除B视图时未调用UITableView函数

时间:2018-02-28 20:09:23

标签: ios swift uitableview

当我使用view B解雇self.dismiss(animated: true, completion: nil)时,view A&{39} numberOfRowsInSectioncellForRowAt未被调用,viewDidLoad也未被调用

这是我的代码,我是UITableView的新手,所以请原谅任何愚蠢的错误。

class WalletTableViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    @IBOutlet weak var segmentedControl: UISegmentedControl!

    @IBOutlet weak var addCryptoButton: UIButton!

    var cryptosArray: [Cryptos] = []

    override func viewDidLoad() {
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        tableView.delegate = self

        loadCryptoArray()
        print("viewDidLoad")
    }
    override func viewWillAppear(_ animated: Bool) {
        tableView.delegate = self

        loadCryptoArray()
        print("viewWillAppear")
    }

    func loadCryptoArray() {
        print("loadCryptoArray")

        if UserDefaults.standard.object(forKey: "cryptosArray") != nil {
            if let decoded = UserDefaults.standard.object(forKey: "cryptosArray") as? Data? {
                let decodedCryptoArray = NSKeyedUnarchiver.unarchiveObject(with: decoded!) as! [Cryptos]
                cryptosArray = decodedCryptoArray
            }
        } else {
            print("UserDefaults KO")
        }
    }

    @IBAction func addCryptoButtonTapped(_ sender: Any) {
        let cryptoPickerViewController = self.storyboard?.instantiateViewController(withIdentifier: "cryptoPickerViewController")
        self.present(cryptoPickerViewController!, animated: true)
    }
    @IBAction func segmentedControlTapped(_ sender: Any) {
        switch segmentedControl.selectedSegmentIndex
        {
        case 0:
            self.dismiss(animated: true, completion: nil)
        case 1:
            self.dismiss(animated: true, completion: nil)
        default:
            break
        }
    }
}

//-----------------------------

extension WalletTableViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cryptosArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let crypto = cryptosArray[indexPath.row]

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! WalletTableViewCell
        cell.setCrypto(crypto: crypto)
        cell.delegate = self

        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 85
    }
}

1 个答案:

答案 0 :(得分:2)

java.lang.AssertionError: expected [java.lang.NullPointerException: Class cannot be null] but found [java.lang.NullPointerException: Class cannot be null] viewDidLoad的生命周期中被调用一次。假设viewController提交A,那么当B被解雇时,没有理由再次调用B,因为viewDidLoad已经从A加载之前,只是在屏幕上显示。

要从呈现的B返回时重新加载表,只需将其添加到A

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

    tableView.reloadData()
}
只要viewWillAppear出现在屏幕上,就会调用

viewController回调。从B返回时就是这种情况 - 隐藏了A,现在又重新出现了。阅读docs中的更多内容,特别是处理与视图相关的通知

部分
  

使用像viewWillAppear(_:)这样的方法来准备您的观点以显示在屏幕上......