如何通过Model in swift在第二个ViewController表格视图中显示第一个ViewController值

问题描述 投票:0回答:1

我已经使用NSObject创建了地址模型。在第一个视图控制器中,我有一个按钮可以推动第二个视图控制器。

地址模型看起来像这样:

 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")
    }
}
}

在第一视图控制器中,如何向模型中添加以下值以显示在第二视图控制器表视图中:

self.localityName = placemark.locality ?? ""
self.sublocalityName = placemark.subLocality ?? ""
self.zipName = placemark.postalCode ?? ""

在第二视图控制器中,如何在表格视图中显示第一视图控制器的值及其计数

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
 }

编辑:

<<

cell.addressLabel.text

我已经使用NSObject创建了地址模型。在第一视图控制器中,我有按下第二视图控制器的按钮。地址模型如下所示:类ProfileModelUserAddress:NSObject,NSCoding {...
swift uitableview variables model pushviewcontroller
1个回答
1
投票
String
© www.soinside.com 2019 - 2024. All rights reserved.