Xcode 8.2.1 / Swift 3 – 从Plist字典数组中加载Ta​​bleView

我有一个plist,我复制到我的项目在TableView中使用它。 plist加载并通过打印内容和行数到控制台进行validation。 当我构build项目时,我得到一个空白的TableView,但没有数据。 我search了几天,但仍然无法显示。 这是我的代码:

import UIKit class TableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() let filePath = Bundle.main.path(forResource: "directory", ofType: "plist") let employees = NSArray(contentsOfFile: filePath!) as! [[String: String]] func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return (employees.count) } 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 } for item in employees { print(item["Name"] as Any) print(item.count) print(employees.count) } 

这里是一些plist:

 <plist version="1.0"> <array> <dict> <key>Department</key> <string>Operations</string> <key>Position</key> <string>Operator </string> <key>Name</key> <string>John Smith</string> <key>Email</key> <string>john.smith@nospam.net</string> <key>Cell Phone</key> <string>123-456-7890</string> <key>Office Phone</key> <string></string> </dict> <dict> <key>Department</key> <string>Sales</string> <key>Position</key> <string>Salesperson</string> <key>Name</key> <string>Susan Brown</string> <key>Email</key> <string>susan.brown@nospam.net</string> <key>Cell Phone</key> <string>234-567-8901</string> <key>Office Phone</key> <string>345-678-9012</string> </dict> 

这里是代码引用的另一个swift文件:

 import UIKit class TableViewControllerTableViewCell: UITableViewCell { @IBOutlet var nameLabel: UILabel! @IBOutlet var positionLabel: UILabel! 

提前致谢! 认真拉出头发….

看起来你需要设置你的tableView委托和数据源。 viewDidLoad添加到您的viewDidLoad

  self.tableView.delegate = self self.tableView.dataSource = self 

你也想要将你的numberOfRowsInSectioncellForRowAt函数从viewDidLoad移出,并在它们前面添加字override

完整代码:

 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 } }