April 24 2010
Cocoa Tip: Losing UITableView Selection
In porting Dollar Clock from the iPhone to the iPad, I switched from using a flip view (using UIModalTransitionStyleFlipHorizontal) to a popover view (UIPopoverController). But a strange thing happened: the UITableView in the popover lost its initial selection, after viewDidAppear was called. Regardless of what I did, the row was always deselected, and indexPathForSelectedRow would go from returning the correct path to nil. Turns out I needed to reloadData before assigning it an initial selection.
- (void)viewDidAppear:(BOOL)animated {
[preferencesListView reloadData]; // This call was necessary for the UITableView to keep its initial selection.
[preferencesListView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
[self updateInterface];
[super viewDidAppear:animated];
}