添加的Tableview数据没有来,为什么在swift中? [英] Added Tableview data is not coming, why in swift?

查看:15
本文介绍了添加的Tableview数据没有来,为什么在swift中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将数据从第三个视图控制器发送到第一个视图控制器,现在是第一次将数据添加到 tableview,但是如果从第一个视图控制器转到另一个视图控制器,然后我回到第一个视图控制器然后添加表视图中的数据为什么会来?

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?

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

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
}

添加表格视图后,如果我转到其他视图控制器,如果我回到表格视图,那么附加值不会出现,为什么?请帮忙写代码.

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.

推荐答案

代替所有这些,声明一个全局变量.

Instead of all this, declare a global variable.

var addressArray : [String] = []

然后从任何视图控制器更新值,当您返回 FirstViewController 时,在 viewWillAppear 中重新加载您的 tableView

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

你的 ThirdViewController 现在应该看起来像这样

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

这篇关于添加的Tableview数据没有来,为什么在swift中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆