1

In Add_EditAddressViewController i need to show all added address in tableview, for that i have created one ARRAY and appending values to array in NewZoomAddressViewController to show in tableview but all the time i am getting single row in table view.. so here how to add value to array dynamically without replacing into oldvalue in Add_EditAddressViewController

and navigation is:

Add_EditAddressViewController: butnTap -> ProfileVC: btnTap -> NewZoomAddressViewController: btnTap -> Add_EditAddressViewController

here each time when i come to NewZoomAddressViewController need to append \(self.sublocalityName!) \(localityName!) \(self.zipName!) to addressArray to show in tableview of Add_EditAddressViewController

Note: here i have added this question related code in github: https://github.com/SwiftSamples/AddressBug here in profileVC you need to tap on map or continue Button then it navigates to NewZoomAddressViewController

class Add_EditAddressViewController: UIViewController,DataEnteredDelegate {
@IBOutlet weak var addeditTableview: UITableView!
var addressArray = [String]()

var city: String?
var pincode: String?
var locality: String?

override func viewDidLoad() {
    super.viewDidLoad()
    addeditTableview.register(UINib(nibName: "EditAddressTableViewCell", bundle: nil), forCellReuseIdentifier: "EditAddressTableViewCell")
    print("zoooom valuew \(pincode)")
    addeditTableview.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.editButton.addTarget(self, action: #selector(editbuttonClicked(sender:)), for: .touchUpInside)

        cell.nameHeader.text = "header"
        cell.addressLabel.text = addressArray[indexPath.row]

    return cell
}
}

NewZoomAddressViewController code:

class NewZoomAddressViewController: UIViewController {
weak var delegate: DataEnteredDelegate? = nil
var addressModel: ProfileModelUserAddress?
var addressArray = [String]()

var zipName: String?
var localityName: String?
var sublocalityName: String?

@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var addressLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    print("in Zoom map VC")
    mapView.delegate = self
    addressLabel.text = "\(self.sublocalityName!) \(localityName!) \(self.zipName!)"

}

@IBAction func confirmBtn(_ sender: Any) {
    let viewController = storyboard?.instantiateViewController(withIdentifier: "Add_EditAddressViewController") as! Add_EditAddressViewController
    addressArray.append("\(sublocalityName ?? "") \(zipName ?? "") \(localityName ?? "")")
    viewController.addressArray = addressArray

    print("total address array all rows \(viewController.addressArray)")
    navigationController?.pushViewController(viewController, animated: true)
}

}

please try to help to display all added address in tableview. i got stuck here from long time.

10
  • how you go from Add_EditAddressViewController to NewZoomAddressViewController through back button ? or you write some code ? Commented Jun 6, 2020 at 6:19
  • @jawadAli Add_EditAddressViewController to profileViewcontroller from here to NewZoomAddressViewController using button pushviewcontroller Commented Jun 6, 2020 at 6:21
  • 1
    so you initiate new NewZoomAddressViewController each time .... thats why array got initiated every time Commented Jun 6, 2020 at 6:22
  • @jawadAli, correct then how do i get all values to table view.. if i am doing in wrong way.. guide me to right way Commented Jun 6, 2020 at 6:25
  • @jawadAli please try to post answer to append each time to addressArray Commented Jun 6, 2020 at 6:50

2 Answers 2

1

In your NewZoomAddressViewController replace confirm button action with

@IBAction func confirmBtn(_ sender: Any) {

    for controller in navigationController?.viewControllers ?? [] {
        if let listController =  controller as? Add_EditAddressViewController {
            let string = "\(sublocalityName ?? "") \(zipName ?? "") \(localityName ?? "")"
            listController.addressArray.append(string)
            navigationController?.popToViewController(controller, animated: true)
            return
        }
    }
}

In Add_EditAddressViewController reload TableView on viewWillAppear

override func viewWillAppear(_ animated: Bool) {
    self.navigationController?.navigationBar.isHidden=true
    addeditTableview.reloadData()
}
Sign up to request clarification or add additional context in comments.

4 Comments

i need to send same addressArray to two view controllers Add_EditAddressViewController, and profile view controller how please let me know
Create a variable in these controllers of type String i.e var array: [String] = [] From you current VC when you initialize object of Add_EditAddressViewControlle & ProfileViewController just pass this array before pushing to navigation.
i am unable to save the added array in Add_EditAddressViewController i will share code in github will you please solve the question as well
i am talking about this question.. i will mark your question as correct answer stackoverflow.com/questions/62259218/…
1

Well what you need to do is to have address array in your profile view as well to pass it to other controller.. so your code becomes

First you will have array in profile like this

class ProfileAddressViewController: UIViewController, CLLocationManagerDelegate, UISearchBarDelegate, DataEnteredDelegate {
   var addressArray = [String]()
}

Then when you call NewZoomAddressViewController you pass that array to them like this

@objc func triggerTouchAction(_ sender: UITapGestureRecognizer) {
        print("Please Help!")

        let viewController = self.storyboard?.instantiateViewController(withIdentifier: "NewZoomAddressViewController") as! NewZoomAddressViewController
            viewController.delegate = self

        viewController.zipName = self.pincodeField.text
        viewController.sublocalityName = self.colonyField.text
        viewController.localityName = self.cityField.text
        viewController.addressArray = addressArray
            self.navigationController?.pushViewController(viewController, animated: true);
    }

And in your Add_EditAddressViewController where you call profile.. assign array to profile

@objc func editbuttonClicked(sender: UIButton) {
        print("in button")
         let viewController = self.storyboard?.instantiateViewController(withIdentifier: "ProfileAddressViewController") as! ProfileAddressViewController
        viewController.addressArray = addressArray
        self.navigationController?.pushViewController(viewController, animated: true)
    }

1 Comment

thank you, here also new value replaces in tableview

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.