dqmoto
dqmoto

Reputation: 13

Getting values from plist into my tableView Dictionaries and Arrays

I am trying to get the Dictionary key values into my TableView. The data source is a plist.

The plist is setup like this: I can't post images yet unfortunately.

Root = Array
 Item 0 = Dictionary
  modelName = "SomeName"
  modelType = "SomeType"
 Item 1 = Dictionary (similar key : values)
 Item 2 = Dictionary 
 Item 3 = Dictionary 

So it's basically an Array of Dictionaries. I want to iterate through the array and use the "modelName" value in my TableView label.

I can get the the individual Array object to print when I use println

if let path = NSBundle.mainBundle().pathForResource("models", ofType:    "plist") {
        if let array = NSArray(contentsOfFile: path) {
        self.tableData = array
        println(self.tableData[0])
        }
    }

but I want to convert my data and call a specific key value to use in my tableView. I'm trying to create a Dictionary for each Item in my plist

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as DataSheetTableViewCell

   var dict = NSDictionary(objectsAndKeys: self.tableData[indexPath.row])
    cell.titleLabel.text = dict["modelName"] as? String

    return cell
}

I'm a bit confused on how to get my plist Dictionary created. Thank you.

Upvotes: 0

Views: 666

Answers (1)

rdelmar
rdelmar

Reputation: 104082

If your plist is an array of dictionaries, then an individual dictionary is just one of the elements of the array which you get with objectAtIndex: (or it's equivalent using subscript syntax). You only need to cast it to a dictionary, not create a new dictionary which is what dictionaryWithObjectsAndKeys does.

var dict = self.tableData[indexPath.row] as NSDictionary

Upvotes: 0

Related Questions