Boas! Estou com dificuldade em receber dados de subitens de minha plist. Observem…
Esta é minha directory.plist
:
Eu quero jogar tudo isso aqui nesta Main.storyboard
:
Sendo que as keys Position
e Name
ficariam na primeira TableViewController e as keys Functionary
, ImageFace
e Phone
ficariam na segunda TableViewController.
Para isso eu fiz o seguinte:
Adicionei ao AppDelegate o seguinte:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { if let url = Bundle.main.url(forResource: "directory", withExtension: "plist"), let array = NSArray(contentsOf: url) as? [[String:Any]] { Shared.instance.employees = array.map{Employee(dictionary: $0)} } return true
Minha
Struct
ficou dessa forma:struct EmployeeDetails { let functionary: String let imageFace: String let phone: String init(dictionary: [String: Any]) { self.functionary = (dictionary["Functionary"] as? String) ?? "" self.imageFace = (dictionary["ImageFace"] as? String) ?? "" self.phone = (dictionary["Phone"] as? String) ?? "" } } struct Employee { let position: String let name: String let details: [EmployeeDetails] // [String:Any] init(dictionary: [String: Any]) { self.position = (dictionary["Position"] as? String) ?? "" self.name = (dictionary["Name"] as? String) ?? "" let t = (dictionary["Details"] as? [Any]) ?? [] self.details = t.map({EmployeeDetails(dictionary: $0 as! [String : Any])}) } } struct Shared { static var instance = Shared() var employees: [Employee] = [] }
Meu primeiro TableViewController, ficou assim:
class Page1: UITableViewController { override func viewDidLoad() { super.viewDidLoad() let anEmployee = Shared.instance.employees[1] print("Name:", anEmployee.name) print("Position:", anEmployee.position) anEmployee.details.forEach({ print("Functionary:", $0.functionary) print("ImageFace:", $0.imageFace) print("Phone:", $0.phone) }) } override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return Shared.instance.employees.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell1 cell.nameLabel.text = Shared.instance.employees[indexPath.row].name cell.positionLabel.text = Shared.instance.employees[indexPath.row].position return cell } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let destination = segue.destination as? Page2, let indexPath = tableView.indexPathForSelectedRow { destination.newPage = Shared.instance.employees[indexPath.row].details[indexPath.row] tableView .deselectRow(at: indexPath, animated: true) } } }
Meu segundo TableViewController, ficou assim:
var newPage: EmployeeDetails! //recently added class Page2: UITableViewController { var newPage: EmployeeDetails! override func viewDidLoad() { super.viewDidLoad() if let theEmployee = newPage { self.title = theEmployee.name } } override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if let theEmployee = newPage { return theEmployee.details.count } return 0 } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell2 if let theEmployee = newPage { cell.faceImage.image = theEmployee.details[indexPath.row].imageFace as? UIImage // did not work cell.functionary.text = theEmployee.details[indexPath.row].functionary cell.phoneLabel.text = theEmployee.details[indexPath.row].phone } return cell } }
Após as alterações, o projeto não apresenta mais erros, porém a 2ª TableViewController permanece vazia! A Debug Area diz o seguinte:
2017-03-28 17:16:28.456 plist sample[7138:425253] Unknown class Page2 in Interface Builder file.
Alguma ideia?
Desde já, agradeço.
struct Employee