Xcode 8.2.1 / Swift 3 - 从Plist数组字典加载TableView

时间:2017-02-08 22:36:11

标签: ios iphone xcode uitableview

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

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!

提前致谢!认真地拔头发....

1 个答案:

答案 0 :(得分:3)

看起来您需要设置tableView委托和数据源。将以下行添加到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
    }
}