问题
I am trying to send values from one view controller - NewZoomAddressViewController
to another one - Add_EditAddressViewController
. I am sending the values to an array in the second view controller (addressArray
).
Here is the first view controller:
class NewZoomAddressViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var addressLabel: UILabel!
var addressArray = [String]()
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
navigationController?.pushViewController(viewController, animated: true)
}
}
Then here I am adding values to addressArray
from NewZoomAddressViewController
like below:
class Add_EditAddressViewController: UIViewController,DataEnteredDelegate {
@IBOutlet var addressEDITTableview: UITableView!
var addressArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
addressEDITTableview.register(UINib(nibName: "EditAddressTableViewCell", bundle: nil), forCellReuseIdentifier: "EditAddressTableViewCell")
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]//"\(city) \(pincode) \(locality)"
return cell
}
}
However, in the table view I am only getting one row. The first time, the single row is okay, but if I add another address - the row is replaced with the new address, and I am no longer getting the old address. How do I show all of the added addresses in the table view?
回答1:
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
}
}
回答2:
addressArray
gets deallocated everytime you move back to the screen. You should declare it in Add_EditAddressViewController
and append the data there only then send it to otherviewcontroller
var addressArray : [String] = []
@IBAction func confirmBtn(_ sender: Any) {
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Add_EditAddressViewController") as! Add_EditAddressViewController;
self.addressArray.append("\(sublocalityName ?? "") \(zipName ?? "") \(localityName ?? "")")
vc.addressArray = self.addressArray
self.navigationController?.pushViewController(viewController, animated: true);
}
And then in your otherViewController
var addressArray : [String] = [] {
didSet {
self.addressEDITTableview.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
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
}
}
来源:https://stackoverflow.com/questions/62132321/how-can-i-update-the-numberofrowsinsection-and-how-do-i-show-all-added-values-in