iPhone developer tip: Smarter selection visuals with UITableView

Many Apple and third-party iPhone applications use UINavigationController and UITableView to navigate through an application’s data hierarchy.

But not all of the niceties are done for you. Here are some tweaks I’ve made manually:

Maintain the user’s scroll position

The scroll position within a UITableView is normally maintained between redisplays, but if you run low on memory and your app starts freeing its offscreen controllers’ views, your scroll position will be reset when the tableview is reloaded and redisplayed.

In your tableview controller, when the user selects a row, before pushing the next controller, save the current scroll position:

CGPoint savedScrollPosition = CGPointZero;
//...
savedScrollPosition = [tableView contentOffset];

Then restore it in viewDidAppear for when the user returns to the tableview:

[tableView setContentOffset:savedScrollPosition animated:NO];

(in the upcoming Instapaper 1.3)

Show the deselect-row animation when you return to the list

In your navigation controller’s viewDidAppear callback, use deselectRowAtIndexPath:animated: on the currently selected row.

This helps orient users in the list and remind them which entry they were just viewing.

(in Apple’s sample code)

Force the selection animation to happen

If you push a new view controller when the user selects a table row, and the user releases the tap too quickly, the blue selection bar never appears on the selected row.

Instead of performing your “open” action immediately during tableView:didSelectRowAtIndexPath, you can delay it by enough time to let the selection animation happen with performSelector:withObject:afterDelay. I’m still fine-tuning the ideal delay, but I think 0.1 seconds is a reasonable estimate.

(in the upcoming Instapaper 1.3)

Related Posts