11

我的 xcode 项目中有一个 UITableview。其中列出了评论。如何通过滚动动画关注 TableView 的最后一个单元格?

4

3 回答 3

26

下面的方法将找到您的表格视图的最后一个索引,并将通过动画聚焦到该单元格

-(void)goToBottom
{
    NSIndexPath *lastIndexPath = [self lastIndexPath];

    [<YOUR TABLE VIEW> scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

查找表格视图的最后一个索引的代码。

-(NSIndexPath *)lastIndexPath
{
    NSInteger lastSectionIndex = MAX(0, [<YOUR TABLE VIEW> numberOfSections] - 1);
    NSInteger lastRowIndex = MAX(0, [<YOUR TABLE VIEW> numberOfRowsInSection:lastSectionIndex] - 1);
    return [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
}

在视图控制器的某处添加此代码

[self performSelector:@selector(goToBottom) withObject:nil afterDelay:1.0];
于 2013-02-07T04:10:33.507 回答
2
于 2013-02-07T04:38:58.133 回答
0

Swift 3:

let indexPath = IndexPath(row: /*ROW TO SCROLL TO*/, section: /*SECTION TO SCROLL TO*/)
tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
于 2017-08-10T17:23:43.327 回答