Thursday, 30 September 2010

A UITableView with an "empty" view

Most every iOS application that presents a list of information is built around the UITableView class. It's a central part of the iPhone UI arsenal.

The really polished apps use this class particularly well. Look at the iPod application, for example. When there is nothing to display in a table view, the iPod app shows a placeholder view with a pretty icon and some text explaining why the list is empty:


It's these neat little UI touches that set a truly great application apart from a merely adequate one.

I want one of those

It's not rocket science to code such UI behaviour, but why do that when someone's already done it for you?

Mosey on over to the TableViewWithEmptyView Gitorious project and grab my code from there.

Most of that project's files comprise the requisite scaffolding to provide an example demonstrating the class. To use the class in your own project you need only copy over the PGTableViewWithEmptyView.h and PGTableViewWithEmptyView.m files.

Using the class is simple. It works exactly like a standard UITableView, but has one extra property emptyView. Assign this property the UIView you want displayed when there are no items in the table. The rest happens automatically for you.

The empty view fades in and out smoothly for added UI chintz.

To make things even easier, you can wire all this up in your XIB file, so your code need never mention the PGTableViewWithEmptyView class itself. Now that's magic.

I'd be grateful to hear from you if you use this class. Please also feel free to send me any suggestions, improvements, or bug reports.

4 comments:

Anonymous said...

this is what I am looking for, empty view but I am not sure how to implement your code in mine? How do you configure the xib by itself?

David Kettler said...
This comment has been removed by the author.
Rafa Vega said...

Using in my new app. Thanks for sharing! :)

Anonymous said...

This is exactly the effect I have been trying to achive! Unfortunately , I have not been able to get this to work programatically. I have experience with table view controllers, but I have not used a table view as the view of a (regular) viewcontroller. I have rather complex tableview contoller working well. Is there any way to integate this technique? I feel like i'm missing something!

Thank you