How do I populate two sections in a tableview with two different arrays using swift? How do I populate two sections in a tableview with two different arrays using swift? swift swift

How do I populate two sections in a tableview with two different arrays using swift?


TableView Cells

You could use a multidimensional array. For example:

let data = [["0,0", "0,1", "0,2"], ["1,0", "1,1", "1,2"]]

For the number of sections use:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {    return data.count}

Then, to specify the number of rows in each section use:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {    return data[section].count}

Finally, you need to setup your cells:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {    let cellText = data[indexPath.section][indexPath.row]    //  Now do whatever you were going to do with the title.}

TableView Headers

You could again use an array, but with just one dimension this time:

let headerTitles = ["Some Data 1", "KickAss"]

Now to set the titles for the sections:

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {    if section < headerTitles.count {        return headerTitles[section]    }    return nil}

The code above checks to see there's a title for that section and returns it, otherwise nil is returned. There won't be a title if the number of titles in headerTitles is smaller than the number of arrays in data.

The Result

enter image description here


You could create a Struct to hold the data that belongs to a section, as an alternative to my previous answer. For example:

struct SectionData {    let title: String    let data : [String]    var numberOfItems: Int {        return data.count    }    subscript(index: Int) -> String {        return data[index]    }}extension SectionData {    //  Putting a new init method here means we can    //  keep the original, memberwise initaliser.    init(title: String, data: String...) {        self.title = title        self.data  = data    }}

Now in your view controller you could setup your section data like so:

lazy var mySections: [SectionData] = {    let section1 = SectionData(title: "Some Data 1", data: "0, 1", "0, 2", "0, 3")    let section2 = SectionData(title: "KickAss", data: "1, 0", "1, 1", "1, 2")    return [section1, section2]}()

Section Headers

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {    return mySections.count}override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {    return mySections[section].title}

Compared to my previous answer, you now don't have to worry about matching the number of headerTitles to the number of arrays in data.

TableView Cells

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {    return mySections[section].numberOfItems}override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {    let cellTitle = mySections[indexPath.section][indexPath.row]    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell    cell.textLabel?.text = cellTitle    return cell}


You can determine which section you are in by looking at indexPath.section.To specify the titles, override the function

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