Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Grouped TableView with sections programmatically in Swift

I would like to know how to create a grouped/sectioned tableview programmatically. The design I am trying to create is this:

enter image description here

I want there to be a UIImageView at the top with a 160 height, followed by 3 sections, each with a different number of rows. I will then place static labels in each row and then update their labels with my data.

My code for this VC is as follows:

import UIKit

class SelectedFilmTableViewController: UITableViewController {

var film: Film? {
    didSet {
        navigationItem.title = film?.Film_Name
    }
}

var cellId = "cellId"

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.dataSource = self
    tableView.delegate = self
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellId)

}

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

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

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

    let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = film?.Directed_By
    cell.detailTextLabel?.text = film?.Film_Poster_Image_URL

    return cell

}

}

I have tried adding the method to state the numberOfSections etc but I don't seem to see them when I build the app. All I see is a normal looking tableview. Am I missing some setup?

Because I am navigating to this VC programmatically, I need to set this up without using the storyboard.

Any help on how to create the look I need above is appreciated, thanks!

like image 894
Nick89 Avatar asked Sep 05 '16 21:09

Nick89


People also ask

How do I add a section in Swift?

To add a section around some cells, start by placing a Section around it, optionally also adding a header and footer. As an example, we could create a row that holds task data for a reminders app, then create a list view that has two sections: one for important tasks and one for less important tasks.

What is Tableview section?

A table view displays a single column of vertically scrolling content, divided into rows and sections. Each row of a table displays a single piece of information related to your app. Sections let you group related rows together.

How do you give space between Tableview cells in Swift?

The way I achieve adding spacing between cells is to make numberOfSections = "Your array count" and make each section contains only one row. And then define headerView and its height. This works great.


2 Answers

When combining other views with a TableView I prefer to make a UIViewController and use a UITableViewController as an embedded controller.

I committed an example on github check it out

Here is what your table controller code should look like:

class MyTableController : UITableViewController {
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 3
    }

    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if(section == 2) {
            return "Film Information"
        } else {
            return " "
        }
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if(section == 0) {
            return 1
        } else if(section == 1) {
            return 2
        } else {
            return 4
        }
    }

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

        let cell = tableView.dequeueReusableCellWithIdentifier("SomeCell", forIndexPath: indexPath) as UITableViewCell


        return cell

    }
}
like image 106
Brian Gorman Avatar answered Oct 24 '22 10:10

Brian Gorman


First you should return 3 section using the following method

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

and then according to the section number passed to this method return 1,2 and 4 (rows) for each section:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // return if section == 0 return 1 and so on...
}

finally use indexPath passed to this method to return the appropriate content according to section and row stored in indexPath:

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

    let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as UITableViewCell

  // if indexPath.section == 0 {
  //  cell.textLabel?.text = film?.Directed_By
  //  cell.detailTextLabel?.text = film?.Film_Poster_Image_URL
  // } else if ....


    return cell

}
like image 1
chiarotto.alessandro Avatar answered Oct 24 '22 12:10

chiarotto.alessandro