【发布时间】: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