How to scroll a table view after correctly inserting a row at the bottom?

Here is the code I'm using:

//inserting a row at the bottom first
_numberOfRecords++;
[_tableView beginUpdates];
[_tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:_numberOfRecords-1 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
[_tableView endUpdates];
//clear text
_inputField.text = @"";

//then scroll to bottom
CGPoint bottomOffset = CGPointMake(0, _tableView.contentSize.height + 44.0 + _tableView.contentInset.top - _tableView.bounds.size.height);
NSLog(@"%f", _tableView.contentSize.height + 44.0 + _tableView.contentInset.top - _tableView.bounds.size.height);
[_tableView setContentOffset:bottomOffset animated:YES];

This will scroll through the table view in a very strange way. But if I put the scroll code before the insert, it works fine except that it ignores the last row inserted. That is, it scrolls to the second last row instead of scrolling to the last line (of course, because it scrolls before inserting a new roll.)

Therefore, I believe that this code has no problems with the position in which it should scroll. The problem is probably related to inserting rows into a tableview. This disrupts the scrolling animation of the table view.

, . , , , , . tableView . scrollView , , tableView .

scrollView tableView, , Apple tableView, tableView. , scrollView , tableView.

, View ?

+4
2

UITableView's scrollToRowAtIndexPath::

[self.tableView scrollToRowAtIndexPath: atScrollPosition: animated:];
+8

:

[_tableView reloadData];

    //scroll to bottom
double y = _tableView.contentSize.height - _tableView.bounds.size.height;
CGPoint bottomOffset = CGPointMake(0, y);
NSLog(@"after = %f", y);
if (y > -_tableView.contentInset.top)
    [_tableView setContentOffset:bottomOffset animated:YES];

- reloadData endUpdates. , tableView contentSize . , contentInset.top( ), , - - - .

[self.tableView scrollToRowAtIndexPath: inSection: atScrollPosition: animated:];

. . TableViewCell , . , .

, .

+1

All Articles