Search code examples
iosobjective-cuitableviewinsertmaster-detail

iOS UITableView and Master-Detail view


I am working on a Master-Detail like app (for both iPhone and iPad) and I can't reproduce the same behavior of the Notes native app while inserting new item. Besides, I haven't found any interesting help on Google. On insertion I want to redirect the user to the detail view of the newly inserted item. I tried to call the selectRowAtIndexPath: in the didChangeObject: method of the controller like this :

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
   atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  newIndexPath:(NSIndexPath *)newIndexPath
{
    UITableView *tableView = self.tableView;

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
            break;

But nothing happens... may be because the newly inserted item didn't have the time to appear in the table view?

Thanks in advance for any help


Solution

  • Try to add [tableView reloadData]:

    [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
    [tableView reloadData];
    [tableView selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    

    Edit:

    scrollToRowAtIndexPath only highlights the cell, and won't fire didSelectRowAtIndexPath, try to manually call it by:

    [tableView.delegate tableView didSelectRowAtIndexPath:indexPath];