繁体   English   中英

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

[英]Xcode 8.2.1 / Swift 3 - Load TableView from Plist Array of Dictionaries

我有一个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!

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

看起来您需要设置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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM