I have a plist that I copied into my project to use it in a TableView. The plist loads and I verified by printing the contents and number of rows to the console. When I bu
Looks like you need to set your tableView delegate and datasource. Add the following lines to your viewDidLoad
.
self.tableView.delegate = self
self.tableView.dataSource = self
You also want to move your numberOfRowsInSection
and cellForRowAt
functions out of viewDidLoad
and add the word override
in front of them.
Full code:
import UIKit
class TableViewController: UITableViewController {
var filePath: String?
var employees: [[String: String]] = []
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: String]]
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 tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewControllerTableViewCell
cell.nameLabel.text = (employees[indexPath.row]["Name"]) //as! String)
cell.positionLabel.text = (employees[indexPath.row]["Position"]) //as! String)
return cell
}
}