Tutorial: Custom UITable section header using Interface Builder

I saw a lot of tutorials about custom header/footer, or even custom section header, but they were creating controls programatically, in this tutorial I want to show how easy is to use Interface Builder to achive same result.

So start with creating new XCode project:

Choose “Single View Application”:
Zrzut ekranu 2015-01-11 o 10.09.37

Enter name whatever you want:
Zrzut ekranu 2015-01-11 o 10.10.26

And we can start finally. So using Interface Builder create UITable and two custom cells, one for table row, one for section header, also add one UILabel in each (so we can display something):

Zrzut ekranu 2015-01-11 o 10.59.04

If your view looks like above – you’re on good way. Now we need to give our cells a reusable identifier – to get them later:

Zrzut ekranu 2015-01-11 o 10.20.15

As you can see, my row cell has identifier “tableitem” and my header cell has “headeritem”. So let’s connect assets to header file ViewController.h:

and after right-click on table you should have:

Zrzut ekranu 2015-01-11 o 10.23.41

Now we need to add UITableViewDataSource and UITableViewDelegate and place for your data, still in ViewController.h:

Then open ViewController.m and add some code to make it look like this:

As you can see we don’t have cellForRowAtIndexPath and viewForHeaderInSection implemented yet, its only some basic code. There is a strange number in heightForHeaderInSection function, it should be your section header height you set in Interface Builder. If value will be different, your header will have too much space or view will be cut.

Now we need to extend UITableViewCell twice – first for table row, second for section header. So add new file:

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Zrzut ekranu 2015-01-11 o 10.31.41

and second one:

Zrzut ekranu 2015-01-11 o 10.32.32

So now you file browser will look like this:

Zrzut ekranu 2015-01-11 o 10.33.21

Now connect you labels to your new classes, you RowCell.h should look like this:

And SectionHeaderCell.h like this:

Next step is connection from Interface Builder to you vars, but first you need to assign your classes to proper cells, for table row:

Zrzut ekranu 2015-01-11 o 10.48.10

and for section header:

Zrzut ekranu 2015-01-11 o 10.48.44

So now your row cell should be connected to its var:

Zrzut ekranu 2015-01-11 o 10.50.34

So should be section header cell:
Zrzut ekranu 2015-01-11 o 10.51.27Now we can add required imports to ViewController.m:

And add UITable delegate methods:

Some notes here, in cellForRowAtIndexPath as usual you get your cell via dequeueReusableCellWithIdentifier then you need to cast returned object to your RowCell class so instead of using viewWithTag method, you can just use cell.yourAsset. Very similar stuff goes in viewForHeaderInSection but, since this method has to return UIView, not UITableViewCell, we’re returning contentView property of UITableViewCell. Now compile and run your project and you should see:

Zrzut ekranu 2015-01-11 o 10.59.57

Feel free to play with interface assets to achieve look you want. If you want, you can grab whole project on GitHub here.

Leave a Reply