How can I update the numberOfRowsInSection and how do I show all added values in this tableView?

后端 未结 2 1152
旧时难觅i
旧时难觅i 2021-01-26 10:47

I am trying to send values from one view controller - NewZoomAddressViewController to another one - Add_EditAddressViewController. I am sending the val

2条回答
  •  天命终不由人
    2021-01-26 11:17

    Update: Try out the following code on your Xcode-playground and play around with it to see how it works and try to implement the same logic in your code.

    import UIKit
    import PlaygroundSupport
    
    class ViewController: UIViewController {
        var array = [String]()
        override func viewDidLoad() {
            super.viewDidLoad()
            let button = UIButton(type: .contactAdd)
            button.center = view.center
            button.addTarget(self, action: #selector(add), for: .touchUpInside)
            view.addSubview(button)
        }
        @objc func add() {
            array.append("New element \(array.count)")
            let controller = TableViewController()
            controller.array = array
            present(controller, animated: true)
        }
    }
    class TableViewController: UITableViewController {
        var array = [String]()
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            array.count
        }
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = UITableViewCell()
            cell.textLabel?.text = array[indexPath.row]
            return cell
        }
    }
    
    PlaygroundPage.current.setLiveView(ViewController())
    

    Store the array outside the function scope and keep adding new elements to the same array instead of a new instance.

    In InitialViewController with button action:

    var addressArray = [String]()
    
    @IBAction func confirmBtn(_ sender: Any) {
        let viewController = storyboard?.instantiateViewController(withIdentifier: "Add_EditAddressViewController") as! Add_EditAddressViewController
        addressArray.append("\(sublocalityName ?? "") \(zipName ?? "") \(localityName ?? "")")
        viewController.addressArray = addressArray
        navigationController?.pushViewController(viewController, animated: true)
    }
    

    In Add_EditAddressViewController:

    class Add_EditAddressViewController: UIViewController {
        var addressArray = [String]()
        override func viewDidLoad() {
            super.viewDidLoad()
            addressEDITTableview.reloadData()
        }
    }
    
    extension Add_EditAddressViewController: UITableViewDelegate, UITableViewDataSource {
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return addressArray.count
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell: EditAddressTableViewCell = tableView.dequeueReusableCell(withIdentifier: "EditAddressTableViewCell") as! EditAddressTableViewCell
            cell.nameHeader.text = "header"
            cell.addressLabel.text = addressArray[indexPath.row]
            return cell
        }
    }
    

提交回复
热议问题