iOS UITableView滚动到部分的底部

Mos*_*tab 13 xcode uitableview nsindexpath ios

我将在其中制作一个包含2个部分的tableview.我可以通过编程方式向每个部分添加单元格,当我添加时,我使用滚动到tableview的末尾

[_tableView setContentOffset:CGPointMake(0, CGFLOAT_MAX)];
Run Code Online (Sandbox Code Playgroud)

我的问题是如何滚动到0部分的末尾,以便当用户向第0部分添加单元格时,tableview动态滚动到第0部分的最后一个单元格.

谢谢.

Ben*_*etz 22

您可以尝试使用以下代码:

int yourSection = 2;
int lastRow = [tableView numberOfRowsInSection:yourSection] - 1;
[tableView scrollToRowAtIndexPath:[NSIndexPath lastRow inSection:yourSection] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
Run Code Online (Sandbox Code Playgroud)

您将获得节中的行数,然后滚动到该indexPath.


Shi*_*mar 15

以上所有建议都是正确的,至少基于文档.但它在我的情况下不起作用 - 我无法让滚动显示表格视图中的最后一行.我不得不在滚动中添加延迟以使其工作.

- (void)scrollToTheBottom:(BOOL)animated
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowCount-1 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:animated];
}
Run Code Online (Sandbox Code Playgroud)

我将上述称为:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self scrollToTheBottom:YES];
});
Run Code Online (Sandbox Code Playgroud)