【问题标题】:How to display first ViewController values in Second ViewController tableview with Model in swift如何在第二个 ViewController 表格视图中使用模型快速显示第一个 ViewController 值
【发布时间】:2020-05-28 17:52:22
【问题描述】:

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

【问题讨论】:

    标签: swift uitableview variables model pushviewcontroller


    【解决方案1】:

    如果您的要求是为每一行使用一个 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
        }
    }
    

    【讨论】:

    • 如果我像 addressModelArray.append(addressModel?.postalCode ?? "") 一样在 tabelview 的行中分别附加每个字段 comig .. 在 questio 中添加了 tableview 图像 .. 我需要在 cell.textLabel.text 这个标签中的 pincode、sublocality、localitey nema
    • 你的意思是你需要1个单元格中的所有文本?
    • 是的,请分享该代码........我需要一个单元格中的完整文本..
    • 为此,您只需要将字符串附加到数组的第一个元素。
    • 不是 1 行,它是一个 tableview,所以会有很多地址行。如果我选​​择另一个地址,那么如何?我需要在 tableview 中显示任意数量的地址
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多