I've got a UITableView that I want to insert and delete rows in.
If I have an array of 10 values and start with the tableview including all rows, the table displays fine. Then if I want to filter out everything except [0, 1, 5]. I then go through the process of deleting rows at index paths: [2, 3, 4, 6, 7, 8, 9] and the returned number of rows is 3. I don't have to change my cell rendering code, it displays rows: [0, 1, 5].
However, if after having filtered everything, I want to come back to this table and load from persistence the table with rows [0, 1, 5], I'm having trouble. The table loads with number of rows: 3 and renders cells: [0, 1, 2].
I've tried initializing the table in viewDidLoad with 0 rows and then in viewDidAppear calling insertRows:[0, 1, 5] - but this throws an exception:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:909
And I've verified that in viewDidLoad, the number of rows is printing 0, and just before this error is thrown, the number of rows printed is 3.
EDIT If I insert instead [0, 1, 2] - it works... but again... I ultimately need [0, 1, 5] showing up.
- (void) viewDidAppear:(BOOL) animated { [super viewDidAppear:animated]; NSArray *initializing_indeces = @[[NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath indexPathForRow:1 inSection:0], [NSIndexPath indexPathForRow:5 inSection:0]]; [self.userDataTable insertRowsAtIndexPaths:initializing_indeces withRowAnimation:UITableViewRowAnimationAutomatic]; }
- (int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Using different booleans, but the gist: if (viewDidLoad) return 0; if (viewDidAppear || filtered) return 3; if (expanded) return 10; }
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"]; } [cell.textLabel setText:[NSString stringWithFormat:@"%i",indexPath.row]]; return cell; }