如何使用 swift 中的模型在第二个 ViewController 表格视图中显示第一个 ViewController 值

How to display first ViewController values in Second ViewController tableview with Model in swift

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

地址模型如下所示:

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

在第一个视图控制器中如何将以下值添加到模型以在第二个视图中显示 viewcontroller table 视图:

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

在第二个视图控制器中如何在 table 视图中显示第一个视图控制器值及其计数

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 这个标签中的所有字段

如果您的要求是对每一行使用一个 String,那么方法如下:

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