简体   繁体   中英

how to insert property list dictionary in a tableview

I have a problem that maybe someone can help me with. I have a property list file arranged in the form of a dictionary. I want to display the information from the property list in table view. But I want only a certain section to be displayed in the table.

this is how my property list looks like

And as I said, I want to insert only a certain part of the list into the table. For example, I want to display only the information of item 2. I want the part of item 2.1, item 2.2, item 2.3 to be displayed in the table. At the moment what I'm able to do is just view all the information without any filters, everything is shown in the tableview all the data in item 1 and item 3.

This is my code

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

Does anyone know how I can do this?

Couple of quick things to note before we get to a possible solution...

    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

These three lines should be able to be condensed to

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

I would recommend using a guard statement though rather than force unwrapping with ! .

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

Secondly, I dont think Property lists are meant to be used as a data store. I would personally have a JSON file with this information in but that is down to personal preference.

Once you have your dictionary from the list. What you are trying to do is actually really simple. rather than iterating over the whole dictionary. Just select the part you want to work with.

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

Here is the code I have in a playground which is working:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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