views:

139

answers:

1

I'm building a detail view for a UITableView. The detail view contains a UITableView which has two rows, and I want the first one to be selected when the detail view is opened. I have tried to use the method [selectRowAtIndexPath:animated:scrollPosition] in [viewWillAppear]:

- (void) viewWillAppear:(BOOL)animated {
[[self tableView] selectRowAtIndexPath:[NSIndexPath indexPathForRow:kStartDateRow inSection:kSection]
animated:NO
scrollPosition:UITableViewScrollPositionNone];
}

However, the selection is not affected by this, and using it in [viewDidAppear] means the selection changes once the view has moved into place. How can I make the selection before the detail view appears on screen?

You can see an example of the behaviour I want in the Settings app. Go to General > Date & Time > Set Date & Time.

+1  A: 

You don't mention any error but try doing [[self tableView] reloadData] before the selectRowAtIndexPath line.

DyingCactus
That totally worked! Thanks for the answer, but I'm puzzled as to why it worked.
Ziggamorph
In this case, it seems the tableView doesn't get around to calling reloadData itself until after viewWillAppear. This: http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html and this: http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewAndDataModel/TableViewAndDataModel.html#//apple_ref/doc/uid/TP40007451-CH5-SW1 might help to understand.
DyingCactus