views:

27

answers:

1

I have a UITableView that gets populated from CoreData via a controller that implements NSFetchedResultsControllerDelegate. How can I have it automatically select the first row (and fire the tableView:didSelectRowAtIndexPath message)?

The tableview is used for a variety of predicate queries, so I'm suspicious of solutions that work on the UIViewController lifecycle (viewDidLoad, etc), but I'm new to the platform, so I'm open.

I've tried a variety of things, but I'm not sure where in the call stack to put it. I've tried calling cell.selected = true inside tableView:cellForRowAtIndex: method, but that just ends up turning the cell black (and doesn't fire the selected callback method)

A tagent question, with all the delegating and core data protocols, does it imply asynchronous data fetch (multiple threads)? Or is the NSFetchedResultsController calling all its related methods in the same thread? Maybe I'm just scared that if it is async, there would be race conditions that would be tough to troubleshoot later.

A: 

Can't add comments yet to ask what you are trying to do when the cell is selected, so I'm going to have to guess.

If you want to mark the cell as selected then you should be using the accessoryType property, for example:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    ...
}

On the other hand, if you are trying to perform some logic based on selecting the first cell, or even show a new view, then this logic should be in a method which you can simply call once the data has loaded.

s1mm0t
My goal is to add logic inside didSelectRowAtIndexPath to display data in another view. Essentially a drilldown of the data model.My gap is exactly the "method to call once the data is loaded".This turns out to be a "duh." I'm stuck in my webdeveloper ajax rut where table loads happen asynchronously, so I never considered the obvious that I should just find the first row and select it immediately after calling fetchedResultsController.performFetch:Thanks for knocking me loose.
deafgreatdane