Tag Archives: UITableViewCell

How to dequeue UITableViewCells

Sometimes Xcode does a bit of magic behind the scenes which I don’t really understand.

Consider this standard table view method to display cells:

Where do we actually ever create a UITableViewCell? All we’re asking here is “do we have reusable ones available” – but if the answer is “no”, then somehow these get created without an error message. Other times (such as when using an overlay table view courtesy of the search display controller) we get an error message.

This brings me to the point that perhaps Xcode and iOS create as well as dequeue cells automatically – but when they don’t, here’s how we can do this manually.

Let’s amend the above code snippet:

Now we properly create a cell should a dequeueable one not be available.

I haven’t had to use this, and the if statement never seems to get called if inserted into the Master/Detail template, yet I find this may be something to keep in mind for future projects.

iOS 6 Table View crashes when deployed to iOS 5

Apple have changed the UITableViewController template in iOS 6 a bit. Specifically, when you create a new UITableViewController class, it’s created using something like this (in the cellForRowAtIndexPath method):

When run in iOS 5 the app crashes. The culprit seems to be the addition of forIndexPath:indexPath in this declaration which is only available in iOS 6. To make it work in either iOS version, simply take it out, like so:

The full code from an iOS 5 template is:

How to change a cell’s selection colour

By default, when you touch a cell in a UITableView, it lights up bright blue. You can use this property to change the cell’s selection style in the cellForRowAtIndexPath method:

Possible values are

  • UITableViewCellSelectionStyleGray
  • UITableViewCellSelectionStyleBlue (the default)
  • UITableViewCellSelectionStyleNone

Alternatively you can select the cell in Interface Builder and pick these values from the drop down menu:

Screen Shot 2013-01-11 at 01.05.19

How to create a transparent cell in a UITableView

First we make the cell’s background transparent. Next we create a custom view which we can show behind the cell, like so:

How to add a cutsom accessory image to a Table View Cell

Each table view cell has an accessory type on the right. You can check which one should be shown by default in Interface Builder (those are none, tick box (aka checkmark), disclosure and detail disclosure chevrons). You can however use your own graphics in this place.

All we need to do is add our own UIImageView as a subview to the cell’s view. Sounds complex, I know – but it’s just as easy as adding a background image to a table view.

We add our custom accessory image in the tableView:cellForRowAtIndexPath method like so:

Make sure you add this before the return cell call.

How to add a background image to a table view

The easiest way to do this is by adding a UIImage to your table. Rather than instantiating a new table object you can simply add the following line into the first Table View Data Source method you have – such as this one:

Just make sure you do this before the return statement (which ends the method, ignoring all code beyond).

Note that in grouped tables the cell object has its own background. You can set its colour in Interface Builder (use clearColour if you’d like your background to shine through) or you can use the tableView:cellForRowAtIndexPath method to override it separately with something else:

Again make sure you add this statement before the return cell call.