views:

1943

answers:

6

UITableView is a very powerful class, powering many navigation and preference views on iPhone. Many people have come up with useful UITableView tips, tricks and samples:

  • various ways to use Interface Builder for table cells
  • how to create preference-style cells
  • ensuring good scrolling speed
  • etc.

Please post your favourite tips on using UITableView, one tip per question. I'll start by posting the ones I found on Stack Overflow and the ones from my bookmarks.

+3  A: 

Implementing “checkmarks” in editing mode to manipulate several rows at once: “Multiple row selection and editing in a UITableView” from the great Cocoa With Love blog.

Andrey Tarantsov
A: 

Implementing Preferences-style grouped tables: Three20 library (extracted from Facebook iPhone app) has a set of ready-made cells that contain various controls.

(Not sure you want to use them, however. Three20 suffers from “not-invented-here” a little bit and tries to subclass and extend everything, so adding it adds quite a bit of a mess to your project. But at least you can use it as an example.)

Andrey Tarantsov
I also feel the same way about Three20. I'm not really willing to integrate it straight into my projects as they are trying to reinvent the wheel everytime something doesn't work the way they want. Although, have to admit the guy behind it has some serious Cocoa mojo and you can definitely get inspired by some of his tricks.
François P.
Yeah, I used it for thumb views, but just re-implemented it myself after a while. To much trouble for such a simple use. It is quite a library though.
Corey Floyd
+4  A: 

Ever wondered what UITableViewController really does?

  • In viewWillAppear, it deselects any selected rows, with animated:YES.

    This by the way is why when you navigate back in UINavigationController, the row you've previously touched is nicely deselected with animation. When you pushed a new view controller onto UINavigationController, you've left the row selected. When you pop it and go back to the table view, viewWillAppear fires and deselects the row. UINavigationController does not even know about this happening.

  • In viewWillAppear, it calls reloadData if the table view contains no rows.

  • In viewDidAppear, it calls flashScrollIndicators.

  • It monitors the keyboard appearing and disappearing and resizes the table view appropriately so that when you tap a text field in a table view, it remains visible after the keyboard appears.

If you don't need the keyboard monitoring behaviour, it is fairly easy to do everything else yourself if you need to.

Andrey Tarantsov
I've never seen the table moving out of the way of the keyboard for free, I had to implement this myself. Any trick to make it do this?
Corey Floyd
@Corey Hm. UITableViewController contains the code that does this, I assumed it just works out of the box. Will check how to make it happen, thanks.
Andrey Tarantsov
The trick is to call super when overriding those methods.
Johan Kool
A: 

How to use Interface Builder to author your cells? Find answers on a separate Stack Overflow discussion: “Using Interface Builder for UITableView’s

Andrey Tarantsov
+1  A: 

How to implement a custom cell background view?

An excellent sample class by Mike Akers in “How to customize the background/border colors of a grouped table view?” on Stack Overflow.

Andrey Tarantsov
+2  A: 

To ensure good performance scrolling, it's important to avoid transparency in any element if possible - so if you are creating custom cells make them all opaque and set the backgrounds correctly.

You can use the Core Animation Performance Tool to visually see how much transparency you have going on in a cell - you have to be running on the device to use this tool.

Kendall Helmstetter Gelner