It really drives me crazy.
I have a table view that displays a sorted list of clients. Users can add a new client, so I need 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 insertion can occur off-screen, which is not very nice. User experience. So my idea was to scroll the table view to the index path where the insert will be inserted before inserting the row:
- (void)finishedSavingNewCustomer:(Customer*)customer atIndex:(NSInteger)index {
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];
NSArray* indexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:index inSection:0]];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}
This code really works as expected: scrolling as a table and inserting a new line correctly.
"" , .. . , .
: 1 - 5:
--------- screen top --------
Row 1
Row 2
Row 3
Row 4
Row 5
------- screen bottom -------
Row 6
Row 7
Row 8
Row 9
:
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 -------
, , 1-5 , .
- - ? ? , , ?
: , , ! , , - .