如何在表视图中插入 属性 列表字典

how to insert property list dictionary in a tableview

我有一个问题,也许有人可以帮助我。 我有一个以字典形式排列的 属性 列表文件。我想在 table 视图中显示来自 属性 列表的信息。但我只想在 table.

中显示某个部分

this is how my property list looks like

正如我所说,我只想将列表的特定部分插入 table。比如我只想显示item 2的信息。 我想让项目2.1、项目2.2、项目2.3的部分显示在table中。 目前我能做的只是查看所有信息,没有任何过滤器,所有内容都显示在table查看第 1 项和第 3 项中的所有数据。

这是我的代码

import UIKit

class PlcStaViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var Serch: UITextField!
    @IBOutlet weak var StationTable: UITableView!

    let cellReuseId = "cell"
    var policeName:[String] = []
    var myRawData:[String:[String:[String:NSObject]]] = [:]
    override func viewDidLoad() {
        super.viewDidLoad()

        self.StationTable.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseId)

        getData()
        StationTable.delegate = self
        StationTable.dataSource = self
    }

    fileprivate func getData ()
    {
        //get plist
        let myPlist = Bundle.main.path(forResource: "Property List", ofType: "plist")!
        let StationData = NSDictionary(contentsOfFile: myPlist)

        //get raw data
        myRawData = StationData as!Dictionary
        //print (myRawData)

        //get data from first key
        let Stations:[String:NSObject] = StationData as! Dictionary
        for singleName in Stations
        {
            //print(singleName.key)
            //policeName.append(singleName.key)

            let StatData:[String:[String:NSObject]] = singleName.value as! Dictionary
            for singelStat in StatData
            {
                //print (singelStat.key)
                policeName.append(singelStat.key)
            }

        }
    }

    @IBAction func SerchBtn(_ sender: UIButton) {
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.policeName.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let myCell:UITableViewCell = (StationTable.dequeueReusableCell(withIdentifier: cellReuseId) as UITableViewCell?)!
        myCell.textLabel?.text = policeName[indexPath.row]


        return myCell
    }    
}

有人知道我该怎么做吗?

在我们找到可能的解决方案之前需要注意几件事...

    let StationData = NSDictionary(contentsOfFile: myPlist)

    //get raw data
    myRawData = StationData as!Dictionary
    //print (myRawData)

    //get data from first key
    let Stations:[String:NSObject] = StationData as! Dictionary

这三行应该可以浓缩成

 let stationData = NSDictionary(contentsOfFile: myPlist) as? [String: Any]

我建议使用保护语句,而不是使用 ! 强制解包。

guard let stationData = NSDictionary(contentsOfFile: myPlist) as? [String: Any] else {
   print("[DEBUG] - station data was not set")
   return 
}

其次,我认为 属性 列表不应该用作数据存储。我个人会有一个包含此信息的 JSON 文件,但这取决于个人喜好。

从列表中获得字典后。你想做的其实很简单。而不是遍历整个字典。只是 select 您想要使用的部分。

if let item2 = stationData["item 2"] as? [String: Any] {
    // process item 2
}

这是我在操场上运行的代码:

guard let myPlist = Bundle.main.path(forResource: "Property List", ofType: "plist") else {
        print("[DEBUG] - myPlist not found")
        return
    }

    guard let stationData = NSDictionary(contentsOfFile: myPlist) as? [String: Any] else {
        print("[DEBUG] - station data not loaded")
        return
    }

    if let item2 = stationData["item 2"] as? [String: Any] {
        for (key, value) in item2 {
            print("\(key) \(value)")
        }
    } else {
        print("[DEBUG] - item 2 not loaded")
    }