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.
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:
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:
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: