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.
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.
indexPath(for:)Returns an index path that represents the row and section of a specified table-view cell.
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.
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 } }
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With