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.

Swift
  • 1,074
  • 12
  • 46
  • how you go from `Add_EditAddressViewController` to `NewZoomAddressViewController` through back button ? or you write some code ? – Jawad Ali Jun 06 '20 at 06:19
  • @jawadAli `Add_EditAddressViewController ` to `profileViewcontroller` from here to `NewZoomAddressViewController ` using button pushviewcontroller – Swift Jun 06 '20 at 06:21
  • 1
    so you initiate new `NewZoomAddressViewController` each time .... thats why array got initiated every time – Jawad Ali Jun 06 '20 at 06: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 – Swift Jun 06 '20 at 06:25
  • @jawadAli please try to post answer to append each time to `addressArray ` – Swift Jun 06 '20 at 06:50
  • @jawadAli could you show us the code for appending `"\(sublocalityName ?? "") \(zipName ?? "") \(localityName ?? "")"` each time to array to show in `Add_EditAddressViewController ` – Swift Jun 06 '20 at 07:29
  • @iPhoneIOS Also mention about the flow, how user moves from one ViewController to another. – udbhateja Jun 06 '20 at 07:33
  • show your complete navigation – Jawad Ali Jun 06 '20 at 07:46
  • @udbhateja, i have edited and added only this code in github, please have a look and do help with code – Swift Jun 06 '20 at 09:06
  • @jawadAli, edited my post with complete navigation, try to help with code – Swift Jun 06 '20 at 09:07

2 Answers2

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)
    }
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
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()
}
udbhateja
  • 948
  • 6
  • 21
  • i need to send same `addressArray` to two view controllers Add_EditAddressViewController, and profile view controller how please let me know – Swift Jun 10 '20 at 14:50
  • 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. – udbhateja Jun 12 '20 at 10:38
  • 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 – Swift Jun 15 '20 at 10:06
  • i am talking about this question.. i will mark your question as correct answer https://stackoverflow.com/questions/62259218/added-tableview-data-is-not-coming-why-in-swift – Swift Jun 15 '20 at 10:07