Daniel Levi Daniel Levi - 6 months ago 15
iOS Question

How to display the array in dictionary - Swift

I started programming

Swift
and I have a problem that I don't know how to solve.

I made a Plist File:

<dict>
<key>Vegetables</key>
<array>
<string>Pepper</string>
<string>Tomato</string>
<string>Cucumber</string>
</array>
<key>Fruits</key>
<array>
<string>Apple</string>
<string>Banana</string>
<string>Watermelon</string>
</array>
</dict>


Now I want to display all Arrays(Fruits and Vegetables) that in the root (Dictionary) in
TableView
so that each Array name(Key) will be a separate
Section and every String will be a single row in my
TableView
.

Here is my view controller class:

import UIKit
//I add TableView methods

class ViewController : UIViewController , UITableViewDelegate ,UITableViewDataSource {

override func viewDidLoad(){
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

// return number of section in my case it's two section (fruits and vegetables)
return 1;
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// number of row in every section is three
// I want to do something like Array.count
return 1;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let row : UITableViewCell = UITableViewCell();
//Add every Row
return row;
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

// Here i want to add header to every section i made;
// Header Name = Array (key) Fruit/Vegetables
// How i do this ?
return "";
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
}

Answer

Write like below code and you will get your answer,

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
var listDic : NSMutableDictionary = NSMutableDictionary()
var headerArr : NSMutableArray = NSMutableArray()

@IBOutlet var tblList : UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.loadData()
    self.navigationController?.navigationBar.translucent = true
    self.navigationController?.navigationBar.tintColor = UIColor.grayColor()

}

func loadData()
{
    if let path = NSBundle.mainBundle().pathForResource("test", ofType: "plist") // Rename with your plist file name
    {
        self.listDic = NSMutableDictionary(contentsOfFile: path)! // NSMutableArray(contentsOfFile: path)!
    }

    if(self.listDic.count > 0)
    {
        self.headerArr.addObjectsFromArray(self.listDic.allKeys)
    }

    self.tblList.reloadData()
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.headerArr.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    let arr = self.listDic.valueForKey(self.headerArr.objectAtIndex(section) as! String) as! NSMutableArray
    return arr.count
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return self.headerArr.objectAtIndex(section) as? String
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: .Default, reuseIdentifier: "Cell")

    let arr = self.listDic.valueForKey(self.headerArr.objectAtIndex(indexPath.section) as! String) as! NSMutableArray

    cell.textLabel?.text = arr.objectAtIndex(indexPath.row) as? String
    return cell
}
}

This is based on your plist file and working fine.

Hope this will help you.