I have created address model with NSObject. in first view controller i have button to push second viewcontroller.
Address Model look like this:
class ProfileModelUserAddress : NSObject, NSCoding{
var pincode : String!
var city : String!
var streetName : String!
init(fromDictionary dictionary: [String:Any]){
pincode = dictionary["pincode"] as? String
city = dictionary["city"] as? String
streetName = dictionary["streetName"] as? String
}
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if pincode != nil{
dictionary["pincode"] = pincode
}
if city != nil{
dictionary["city"] = city
}
if streetName != nil{
dictionary["streetName"] = streetName
}
return dictionary
}
@objc required init(coder aDecoder: NSCoder)
{
pincode = aDecoder.decodeObject(forKey: "pincode") as? String
city = aDecoder.decodeObject(forKey: "city") as? String
streetName = aDecoder.decodeObject(forKey: "streetName") as? String
}
@objc func encode(with aCoder: NSCoder)
{
if pincode != nil{
aCoder.encode(pincode, forKey: "pincode")
}
if city != nil{
aCoder.encode(city, forKey: "city")
}
if streetName != nil{
aCoder.encode(streetName, forKey: "streetName")
}
}
}
in first view controller how to add below values to model to display in second viewcontroller table view:
self.localityName = placemark.locality ?? ""
self.sublocalityName = placemark.subLocality ?? ""
self.zipName = placemark.postalCode ?? ""
In second view controller how to display first view controller values in table view and its count
var addressModel: ProfileModelUserAddress?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return modeladdress.count // here how to add count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: EditAddressTableViewCell = tableView.dequeueReusableCell(withIdentifier: "EditAddressTableViewCell") as! EditAddressTableViewCell
let addr = addressModel.[indexPath.row]
let street = addr?.streetName
city = addr?.city
pincode = addr?.pincode
cell.addressLabel.text = "\(city!) \(pincode!) \(street)"
print("add address tableview cll \(cell.addressLabel.text)")
return cell
}
Edit:
if i add like below answer city, pincode, etc.. like every field coming seperatley like below: i need all fields in cell.addressLabel.text
this label
If your requirement is to use a single String
for each row then, here's how:
class SecondViewController: UITableViewController {
var addressModelArray = [ProfileModelUserAddress]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.reloadData()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
addressModelArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: EditAddressTableViewCell = tableView.dequeueReusableCell(withIdentifier: "EditAddressTableViewCell") as! EditAddressTableViewCell
let addressModel = addressModelArray[indexPath.row]
var text = addressModel?.locality ?? ""
text.append(addressModel?.subLocality ?? "")
text.append(addressModel?.postalCode ?? "")
cell.textLabel.text = text
return cell
}
}