Added Tableview data is not coming, why in swift?

怎甘沉沦 提交于 2020-06-28 04:14:31

问题


I am sending data to tableview from 3rd view controller to first view controller, now for the first time data is adding to tableview but if go from 1st view controller to other view controller and i come back to first view controller then the added data in table view is coming why?

in 3rd view controller sending data to 1st view controller table view like below:

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
        }
    }
}

in 1st view controller adding data to tableview like below:

 @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
}

after added table view if i go to other view controller and if i come back to table view thae added value is not coming, why? please do help with code.


回答1:


Instead of iterating through all your ViewControllers to find your VC that contains your table view and passing data back through that reference, I'd recommend using an unwind segue.

Check this link for more information on how to implement unwind segues: https://developer.apple.com/documentation/uikit/resource_management/dismissing_a_view_controller_with_an_unwind_segue

Take a look at this stackoverflow answer for more help: Passing data with unwind segue




回答2:


Because your var addressArray = [String]() always empty until you click @IBAction func confirmBtn(_ sender: Any) {...} this function on 3rd view and pop to Firstview.

Everytime when you go firstviewcontroller(expect 3rd's button action) addressArray variable gonna be empty.

You can fix it with using global array or make a struct and add values to struct's array properties.




回答3:


Instead of all this, declare a global variable.

var addressArray : [String] = []

Then update the values from any view controller, and when you come back to your FirstViewController, in viewWillAppear reload your tableView

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

And your ThirdViewController should look like this now

@IBAction func confirmBtn(_ sender: Any) {
    guard let string = addressLabel.text else { return }
    addressArray.append(string)
    navigationController?.popToViewController(controller, animated: true)
}


来源:https://stackoverflow.com/questions/62259218/added-tableview-data-is-not-coming-why-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!