开发者

UITableView via NSFetchedResultsControllerDelegate, select first record by default?

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 UIVie开发者_StackOverflow中文版wController 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.


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.


Do like this:

if (indexPath.section == 0 && indexPath.row == 0) {
   //select first row by default
   [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
   //then populate detail view with corresponding managedObject
   [self tableView:tableView didSelectRowAtIndexPath: indexPath];
}

Of course, this is assuming you already coded your didSelectRowAtIndexPath properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜