问题描述:

I ran into this problem that I have searched all over the internet for and still could not find the solution. Please look at the code below:

NSLog(@"%i", [self tableView:tableView numberOfRowsInSection:0]); // 4

[self updateCardIDArray];

NSLog(@"%i", [self tableView:tableView numberOfRowsInSection:0]); // 5

int indexOfCreatedCard = [cardIDs indexOfObject:newCardID];

NSLog(@"%i", indexOfCreatedCard); // 4

[tableView reloadData];

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexOfCreatedCard inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:FALSE];

NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects:[NSIndexPath indexPathForRow:indexOfCreatedCard inSection:0], nil];

[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];

Basically, what I am trying to achieve here is scroll the table to the just created cell and then show an animation of it being added in. When I use this code, I have this error:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:1070

and this exception:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.

The thing is, if I remove the scrolling and the reload data lines, the program would work normally (without being able to see the add cell animation, of course). Any help will be appreciated!

网友答案:

Try

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexOfCreatedCard inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:NO];
网友答案:

I think the problem is, u are scrolling before cell is inserted, i just posting the sample code that suits ur problem

 - (IBAction)scrollPlease:(id)sender 
   {
       aTableView.contentInset = UIEdgeInsetsMake(0, 0, 10, 0); //to move some position below 
       k = k + 1; //hear k is the row numbers in ur case  "indexOfCreatedCard"
       [aTableView beginUpdates]; // first u add the cell to tableview 
       [aTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:k-1 inSection:0]] withRowAnimation:UITableViewRowAnimationMiddle];
       [aTableView endUpdates]; //hear (k - 1) is the last row 
       [aTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:k-1 inSection:0] atScrollPosition:UITableViewRowAnimationTop animated:NO]; //then scroll to last cell it will  show the animation of adding the cell 
        aTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); // after u set it to zero 

  }

hope u get some idea :)

相关阅读:
Top