Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I group TableView items from a dictionary in swift?

Tags:

Lets consider this example:

import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {      @IBOutlet weak var tableView: UITableView!      var names = ["Vegetables": ["Tomato", "Potato", "Lettuce"], "Fruits": ["Apple", "Banana"]]          func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{          let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:"test")      return cell     }      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{     return ???     }     func numberOfSectionsInTableView(tableView: UITableView) -> Int{           return names.count     }      func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]!{      return ???     }      func tableView(tableView: UITableView,         titleForHeaderInSection section: Int) -> String?{             return ????     } } 

let's assume that we need that the keys (fruits and vegetables) of the dictionary are the number of sections, plus they will be the titles of the sections. The items of the keys (eg apples and banana) will be the rows of each section. How can I implement this in my code? I know it might be easy but I couldn't figure it out my self.

like image 998
Rami Ammoun Avatar asked Jun 30 '15 10:06

Rami Ammoun


People also ask

Is it possible to add UITableView within a UITableViewCell?

Implementation of adding a table view inside the cell of UItableview aka, Nested Table View. Used the Power of Autosizing table view and delegate to achieve the expansion and collapse of the cell height.

What is indexPath in TableView Swift?

indexPath(for:)Returns an index path that represents the row and section of a specified table-view cell.

What is array of dictionary in Swift?

Arrays are ordered collections of values. Sets are unordered collections of unique values. Dictionaries are unordered collections of key-value associations. Arrays, sets, and dictionaries in Swift are always clear about the types of values and keys that they can store.


2 Answers

You can use struct for that and here is example:

import UIKit  class TableViewController: UITableViewController {      var names = ["Vegetables": ["Tomato", "Potato", "Lettuce"], "Fruits": ["Apple", "Banana"]]      struct Objects {          var sectionName : String!         var sectionObjects : [String]!     }      var objectArray = [Objects]()      override func viewDidLoad() {         super.viewDidLoad()          for (key, value) in names {             println("\(key) -> \(value)")             objectArray.append(Objects(sectionName: key, sectionObjects: value))         }     }      // MARK: - Table view data source      override func numberOfSectionsInTableView(tableView: UITableView) -> Int {         return objectArray.count     }      override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {         return objectArray[section].sectionObjects.count     }       override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {         let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell          // Configure the cell...         cell.textLabel?.text = objectArray[indexPath.section].sectionObjects[indexPath.row]         return cell     }      override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {          return objectArray[section].sectionName     } } 
like image 112
Dharmesh Kheni Avatar answered Sep 21 '22 23:09

Dharmesh Kheni


Swift 2

you dictionary example

var dic:Dictionary<String,String> = ["key":"value","key1":"value2"] 

Your table

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{         let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell      var key   = Array(self.dic.keys)[indexPath.row]     var value = Array(self.dic.values)[indexPath.row]     cell.text = key + value  } 
like image 30
Diego Avatar answered Sep 19 '22 23:09

Diego