
A lot of people have questions about creating custom UITableViewCells and after showing this technique to a friend of mine, Ian, he asked me to write it up so that he and others could refer to it. First let me say this is technique is based on an approach another friend of mine showed me last year. Thanks Kendall.
This article accompanies this youtube video
First let me point out that the default UITableCellView has space for an image on the left, a text string in the middle and a disclosure indicator on the right.

We sometimes want to create a custom cell with more information and in this tutorial we are going to create a very simple cell with two strings We'll only show the state name and capital but this technique can be extended to create as fancy a cell as you like.
Inside our table view controller, I'll create and initialize two NSArray instance variables for the states and capitals. Then in cellForRowAtIndexPath I can set the cells text to our formatted string.
And our application now looks like this:

Now supposed we want to change the layout a bit and have the capital names over the state name. There are three common approaches to doing that:
For the first approach, we need only create the UILabels, add them as subviews and give them a tag so we can find them later.

If we want more control, we can create a subclass of UITableView and manage the views in a similar way in code. This second method is very similar to the way you create other custom views for custom drawing.
The third approach lets us use Interface Builder to layout the cell by following the steps below:

And of course the cell could be enhanced to include population or other state related information or to have different display attributes. I know that can be a little tricky but hopefully you'll be able to follow the steps and the video.
Thanks for sticking with me. I hope I have been able to show you something new. Let me know if you have any comments or questions.
Comments
Jeff Geerling (not verified)
Mon, 02/14/2011 - 18:54
Permalink
Nice tutorial here - makes
Nice tutorial here - makes things a lot easier than doing everything in code! For a more-or-less graphic designer, laying out the table view cells in IB is a very nice thing.
The
initWithFrame:reuseIdentifier:part of your code is deprecated, though (just FYI).Tim Nelson (not verified)
Wed, 03/30/2011 - 13:02
Permalink
Memory Leak
Chased this memory leak for a couple of hours. loadNibNamed ...should have owner:self ... not nil.
Tim Nelson (not verified)
Thu, 03/31/2011 - 11:08
Permalink
Memory Leaks
Actually it is worse than that. If I add owner:self is doesn't seem to matter it still leaks some memory. Accessing the cell in this way seems to leave it open to leaking. I changed my implementation to one described here (using a temporary controller):
http://www.bdunagan.com/2009/06/28/custom-uitableviewcell-from-a-xib-in-...
And that seems to stop the leaking.
Sebastian Misiewicz (not verified)
Thu, 09/29/2011 - 13:21
Permalink
Memory Leaks in-deed
Yep. agree with you 100%! I can't believe no one is screaming about this louder, but this is a major issue and should definitely NOT be a "recommended" way nor it should be encouraged.
Thanks for pointing it out Tim, I thought I was alone on this trying to google my way to a solution ;)
Herbert Altink (not verified)
Tue, 04/05/2011 - 02:52
Permalink
Great tutorial
This is really magnificent! Thanks for this tutorial, it's incredibly easy to use.
Shrini (not verified)
Fri, 04/08/2011 - 02:25
Permalink
Custom cell overlapping issue
Dear Author, I have tried to create custom cell with two images, three labels with last solution given by, I'm seeing overlapping of cells so can you please give some light
francois saab (not verified)
Sun, 04/10/2011 - 08:23
Permalink
great tutorial
thanks ,thats great .
Rob Wilson (not verified)
Tue, 05/31/2011 - 10:18
Permalink
Thanks for the screencast - a
Thanks for the screencast - a nice fast paced video that has helped me identify what I need to do. It would have been nice to see how you add any touch-up listeners, as I am not yet clear whether you add it to the table, the cell or another mechanism.
Dave (not verified)
Mon, 06/06/2011 - 20:25
Permalink
UITableView CustomTableColumns
Thank you for the excellent tutorial it was very easy to follow, even for a beginner like myself.
Is there any chance you could make a small tutorial (hopefully in xcode 4) for a UITableView with Columns that we load data from an array or even just temp data into.
I have read alot of tutorials online and found code examples but none which I would deem easy to follow.
Daniel (not verified)
Wed, 06/08/2011 - 01:58
Permalink
Err
Ill get an error on:
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
with my UILabel Category:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key category.'
i have my uitableview in a TabBar controller.
do you know what i am doing wrong?
cj (not verified)
Thu, 06/09/2011 - 09:15
Permalink
Accessory Button
This was very helpful. Thank you so much for posting.
I see that the accessory button is defined in the nib also -- How do you connect it to the table so that tapping the accessory button performs an action on that table cell? Is it done in your CustomCell class, or a the TableView class. --- I tried doing it programmatically with no success.
Thanks for any help
Jeffrey Walton (not verified)
Mon, 06/27/2011 - 17:35
Permalink
3rd Approach: Purpose of for(...) loop?
Hi Mollom,
What is the purpose of the 'for loop' in the 3rd method? Since you are loading the CustomCell NIB, shouldn't you always get back a CustomCell type cell?
Jeff
Sagar Patel (not verified)
Tue, 07/12/2011 - 01:37
Permalink
To change the orientation
Its really very good Example.. But i want to know that In this application when the change the orientation its not working properly... so please guide me in this...
francois (not verified)
Sat, 08/06/2011 - 23:05
Permalink
thank you for this article,one question
thats great way of customizing a cell using interface builder rather than doing CG coding.....
this technique can be used to custom other custom user controls...
Rien (not verified)
Sun, 08/07/2011 - 02:46
Permalink
Error in method 3
The "isKindOfClass method should compare against [CustomCell class]
Your solution does work as long as there is just one UITableView subclass in the nib though.
Bob Hucker (not verified)
Fri, 08/19/2011 - 13:00
Permalink
Background color
This was extremely helpful. Thanks a lot!
One thing you might add: I found that, to get a gray background on the cell, I had to add another view and set its background color in Interface Builder, then set the labels on top of the view to clearColor background. Setting the top-level custom cell background color did not work.
Matt (not verified)
Fri, 10/14/2011 - 18:23
Permalink
Great tutorial
Thanks for this, it was really well written and easy to follow.
Calin (not verified)
Tue, 11/01/2011 - 15:40
Permalink
Usefull tutorial - Thanks.
Usefull tutorial - Thanks.
Sutsake (not verified)
Thu, 12/01/2011 - 06:36
Permalink
Good Tutorial , Thank you
Good Tutorial , Thank you
Aashish (not verified)
Wed, 12/21/2011 - 00:13
Permalink
Custom Table View
Thank You for this tutorial.
(not verified) (not verified)
Fri, 01/20/2012 - 14:39
Permalink
Thanks.
Thanks.
Add new comment