添加的Tableview数据不来,为什么要这么快?

问题描述 投票:0回答:1

我正在将数据从第三视图控制器发送到表视图到第一视图控制器,现在是第一次将数据添加到表视图,但是如果从第一视图控制器转到其他视图控制器,并且我又回到第一视图控制器,则添加的数据在表视图中为什么会来?

在第三视图控制器中,将数据发送到如下所示的第一视图控制器表视图:

var viewController: UIViewController?

@IBAction func confirmBtn(_ sender: Any) {
   for controller in navigationController?.viewControllers ?? [] {
        if let listController =  controller as? ProfileViewController {
            guard let string = addressLabel.text else { return }//"\(sublocalityName ?? "") \(zipName ?? "") \(localityName ?? "")"
            listController.addressArray.append(string)
            navigationController?.popToViewController(controller, animated: true)
            return
        }
    }
}

在第一个视图控制器中,将数据添加到如下所示的tableview中:

 @IBOutlet weak var addeditTableview: UITableView!
var addressArray = [String]()

 override func viewWillAppear(_ animated: Bool) {
    self.navigationController?.navigationBar.isHidden=true
    addeditTableview.reloadData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return addressArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

   // let cell: EditAddressTableViewCell = addeditTableview.dequeueReusableCell(withIdentifier: "EditAddressTableViewCell") as! EditAddressTableViewCell

    let cell = tableView.dequeueReusableCell(withIdentifier: "EditAddressTableViewCell", for: indexPath) as! EditAddressTableViewCell

    cell.editButton.addTarget(self, action: #selector(editbuttonClicked(sender:)), for: .touchUpInside)
       cell.nameHeader.text = "Other"
        cell.addressLabel.text = addressArray[indexPath.row]//"\(city) \(pincode) \(locality)"

    return cell
}

添加表视图后,如果我转到其他视图控制器,并且如果我返回表视图,那么附加值没有到来,为什么?请提供代码帮助。

swift uitableview uinavigationcontroller
1个回答
0
投票
我不建议遍历所有ViewController来查找包含表视图的VC,并通过该引用将数据传递回去,我建议使用展开顺序。

请检查此链接以获取有关如何实施放宽序列的更多信息:https://developer.apple.com/documentation/uikit/resource_management/dismissing_a_view_controller_with_an_unwind_segue

查看此stackoverflow答案以获取更多帮助:Passing data with unwind segue

© www.soinside.com 2019 - 2024. All rights reserved.