Tag Archives: UITableViewCell

How to return the number of rows in a UITableView with Core Data

Likewise, here’s how we return the number of rows using a fetched results controller – as provided by the Master / Detail template:

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: