1
I’m having trouble loading the items from a Property list. Watch my plist:
Here’s my first View Controller:
import UIKit
class Page1: UITableViewController {
var filePath: String?
var employees: [[String: Any]] = []
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
filePath = Bundle.main.path(forResource: "directory", ofType: "plist")
employees = NSArray(contentsOfFile: filePath!) as! [[String: Any]]
for item in employees {
print(item["Name"] as Any)
print(item.count)
print(employees.count)
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return employees.count
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? Page2,
let indexPath = tableView.indexPathForSelectedRow {
destination.itemSelecionado = employees[indexPath.row]
tableView .deselectRow(at: indexPath, animated: true)
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.nameLabel.text = (employees[indexPath.row]["Name"] as! String)
cell.positionLabel.text = (employees[indexPath.row]["Position"] as! String)
return cell
}
}
Here’s my second View Controller:
import UIKit
class Page2: UITableViewController {
var itemSelecionado: [Page1] = []
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemSelecionado.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell2
cell.emailLabel.text = (itemSelecionado.employees[indexPath.row]["Email"] as! String )
cell.phoneLabel.text = (itemSelecionado.employees[indexPath.row]["Phone"] as! String? )
return cell
}
}
The Xcode is returning me 3 errors, it’s them:
However, I do not understand why these mistakes, someone could help me?
From now on, thank you!
Explain exactly what the result you want, because I could see two possibilities, but that would generate different changes.
– Paulo Rodrigues
I want an app that presents itself in 2 Tableviews. The first will present the name and function of the employee and, when touching a particular employee, the second will present details of the same, phone, email and other items that I will add later.
– Arnon