Jigen
Jigen

Reputation: 465

UITableView dynamic grouped cells

I've created this settings tableview using static cells and grouped style:

enter image description here

I'd like to recreate the same tableview but this time I'd like to use dynamic cells. I know how to work with dynamic cells but I don't know how I can set different sections with different headers.

I need 4 section (status, queue, type, severities) and each section has indefinite number of cells (the number is equal to the length of the array of data obviously).

Can someone post a complete example please? I couldn't find any documentation that could help me.

Upvotes: 4

Views: 1435

Answers (2)

Jeet
Jeet

Reputation: 5659

Here is the code TableViewDemo You basically have to use below two methods.

 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 4
}

and this for title of header -

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "your title" // Use the titleForHeaderInSection index
}

Upvotes: 2

Chris
Chris

Reputation: 147

you have to use numberOfSectionsInTableView, numberOfRowsInSection, titleForHeaderInSection and cellForRowAtIndexPath

let section = ["section1", "section2"]
let cellsInSection = [["section1cell1", "section1cell2"], ["section2cell1", "section2cell2"]]

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

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

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

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

    if indexPath.section == 0 {

        let cell = tableView.dequeueReusableCellWithIdentifier("section1", forIndexPath: indexPath) as! SectionOneTableViewCell

        return cell
    } else if indexPath.section == 1 {

        let cell = tableView.dequeueReusableCellWithIdentifier("section2", forIndexPath: indexPath) as! SectionTwoTableViewCell
        return cell
    }
}

Upvotes: 1

Related Questions