Ios 在tableView:cellForRowAtIndexPath中无法实现这一点,因此我最终放弃了在ScrollView DiEndScrollingAnimation中设置选择,只使用performSelector:withObject:afterDel

Ios 在tableView:cellForRowAtIndexPath中无法实现这一点,因此我最终放弃了在ScrollView DiEndScrollingAnimation中设置选择,只使用performSelector:withObject:afterDel,ios,uitableview,core-data,Ios,Uitableview,Core Data,在tableView:cellForRowAtIndexPath中无法实现这一点,因此我最终放弃了在ScrollView DiEndScrollingAnimation中设置选择,只使用performSelector:withObject:afterDelay:在所有更新完成后运行一个方法来设置选择和滚动位置。 #pragma mark - Fetched results controller delegate - (void)controllerWillChangeContent:(NSFe

在tableView:cellForRowAtIndexPath中无法实现这一点,因此我最终放弃了在ScrollView DiEndScrollingAnimation中设置选择,只使用performSelector:withObject:afterDelay:在所有更新完成后运行一个方法来设置选择和滚动位置。
#pragma mark - Fetched results controller delegate

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
    switch(type)
    {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (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:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationTop];
                [self.tableView selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];

            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{


    [self.tableView endUpdates];

}  
case NSFetchedResultsChangeInsert:
   [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
   [tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
   insertedIndexPath = newIndexPath; //remember for selection
break;
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
   [self.tableView selectRowAtIndexPath:insertedIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
case NSFetchedResultsChangeInsert:
   [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]  withRowAnimation:UITableViewRowAnimationFade];
   //save new index path into your own ivar
   self.newlyInsertIndex = newIndexPath
break;
    if (!self.changeIsUserDriven){

         [self.tableView endUpdates];

        if (self.newlyInsertedIndex){
            //ScrollPositionNone == use least amount of effort to show cell
            [self.tableView scrollToRowAtIndexPath:self.newlyInsertedIndex  
            atScrollPosition:UITableViewScrollPositionNone animated:YES];
            //cleanup
            self.newlyInsertedIndex = Nil;
        }
    }
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
   [self.tableView selectRowAtIndexPath:insertedIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
@implementation MyTableViewController {
    BOOL _selectFirstRow;
}
- (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:[NSArray arrayWithObject:newIndexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            _selectFirstRow = YES;
            break;
- (void) selectFirstTableRow
{
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
                                animated:YES
                          scrollPosition:UITableViewScrollPositionTop];
}
- (void) controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView endUpdates];
    if (_selectFirstRow) {
        [self selectFirstTableRow];
        _selectFirstRow = NO;
    }
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
  [self.tableView endUpdates];

  if (_newObject) {
    NSIndexPath *ip = [self.fetchedResultsController indexPathForObject:_newObject];
    [self.tableView selectRowAtIndexPath:ip animated:YES
                    scrollPosition:UITableViewScrollPositionMiddle];
  }
}