如何使用滚动动画关注UITableview中的最后一个单元格?

Ana*_*nya 11 cocoa-touch objective-c uitableview ios

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

Kan*_*sad 26

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

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

    [<YOUR TABLE VIEW> scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

用于查找tableview的最后一个索引的代码.

-(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];
}
Run Code Online (Sandbox Code Playgroud)

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

[self performSelector:@selector(goToBottom) withObject:nil afterDelay:1.0];
Run Code Online (Sandbox Code Playgroud)