This is really driving me crazy.
I have a table view that displays a sorted list of customers. Users can add a new customer, thus I have to add a new row to the table view (by updating the data model and calling insertRowsAtIndexPaths:withRowAnimation:
). However, since the table view is sorted, this insert can occur off-screen, which is not very nice wrt. user experience. So my idea was to scroll the table view to the index path where the insert will occur before actually inserting the row:
- (void)finishedSavingNewCustomer:(Customer*)customer atIndex:(NSInteger)index {
// NOTE: self.customers (which is the model for the table view used in all datasource
// methods) has already been updated at this point, ie. it already contains the new customer
// scroll the table view so that the insert position is on-screen
NSInteger scrollRow = (index < ([self.customers count] - 1) ? index : [self.customers count] - 2);
NSIndexPath* scrollIndexPath = [NSIndexPath indexPathForRow:scrollRow inSection:0];
[self.tableView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionNone animated:NO];
// insert the new row
NSArray* indexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:index inSection:0]];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}
This code actually works as expected: The table view scrolls and the new row is inserted correctly.
However, after the insert some table view cells become "irresponsive", ie. they won't display the delete button upon swipe and don't even highlight when selected. Since this is really weird and kinda hard to explain, let me try to illustrate it.
Initial situation: Rows 1 - 5 are displayed:
--------- screen top --------
Row 1
Row 2
Row 3
Row 4
Row 5
------- screen bottom -------
Row 6
Row 7
Row 8
Row 9
Situation after scrolling and inserting:
Row 1
Row 2
Row 3
Row 4
Row 5
--------- screen top --------
Row 6
Row 7
Row 7a << newly inserted
Row 8
Row 9
------- screen bottom -------
Now, after the insert rows 1 - 5 will not respond to user interaction as described above.
Anybody ever experienced this kind of problem? Ideas? Are there other approaches to make sure that the insertion of new rows is visible for the user?
Update: Just tested on the device, and it turns out that this problem only occurs on the simulator! This makes the issue less unpleasant, but I'd still be interested if someone else has experienced this behavior.