Weird UITableView behavior after scrolling and inserting a new row

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 {
    // 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 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 , .

- - ? ? , , ?


: , , ! , , - .

+3
1

Simulator - , Core Data, UIAlertViews UIActionSheets .

, Simulator ( , ..).

  • ,
  • - , , .

. , , .

+1

Source: https://habr.com/ru/post/1712414/


All Articles