Ios 如何通过滚动动画聚焦UITableview中的最后一个单元格?

Ios 如何通过滚动动画聚焦UITableview中的最后一个单元格?,ios,objective-c,cocoa-touch,uitableview,Ios,Objective C,Cocoa Touch,Uitableview,我的xcode项目中有一个UITableview。其中列出了注释。如何通过滚动动画关注TableView的最后一个单元格?下面的方法将找到表视图的最后一个索引,并通过动画关注该单元格 -(void)goToBottom { NSIndexPath *lastIndexPath = [self lastIndexPath]; [<YOUR TABLE VIEW> scrollToRowAtIndexPath:lastIndexPath atScrollPosition

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

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

-(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];

请记住,UITableView继承自UIScrollView

-(void)scrollToBottom:(id)sender
{
    CGSize r = self.tableView.contentSize;
    [self.tableView scrollRectToVisible:CGRectMake(0, r.height-10, r.width, 10) animated:YES];
}
像执行一样执行它

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(…);
[button addTarget:self action:@selector(scrollToBottom:) forControlEvents:UIControlEventTouchUpInside];
(您不需要使用
性能选择器:…


另一个解决方案是选择最后一行。表视图将处理其余部分

-(void)scrollToBottom:(id)sender
{
    NSInteger lasSection = [self.tableView numberOfSections] - 1;
    NSInteger lastRow = [self.tableView numberOfRowsInSection:lasSection]-1;

    //this while loops searches for the last section that has more than 0 rows.
    // maybe you dont need this check
    while (lastRow < 0 && lasSection > 0) {
        --lasSection;
        lastRow = [self.tableView numberOfRowsInSection:lasSection]-1;
    }
    //if there is no section with any row. if your data source is sane,
    // this is not needed.
    if (lasSection < 0 && lastRow < 0)
        return;

    NSIndexPath *lastRowIndexPath =[NSIndexPath indexPathForRow:lastRow inSection:lasSection];
    [self.tableView selectRowAtIndexPath:lastRowIndexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
}
-(无效)滚动到底部:(id)发件人
{
NSInteger lasSection=[self.tableView numberOfSections]-1;
NSInteger lastRow=[self.tableView numberOfRowsInSection:lasSection]-1;
//此while循环搜索包含0行以上的最后一个节。
//也许你不需要这张支票
而(lastRow<0&&lasSection>0){
--激光切割;
lastRow=[self.tableView numberOfRowsInSection:lasSection]-1;
}
//如果没有包含任何行的节。如果数据源正常,
//这是不必要的。
if(lasSection<0&&lastRow<0)
返回;
nsindepath*lastRowIndexPath=[nsindepath-indexPathForRow:lastRow-instation:lasSection];
[self.tableView selectRowAtIndexPath:lastRowIndexPath动画:是scrollPosition:UITableViewScrollPositionBottom];
}
按钮是相同的。

Swift 3:

let indexPath = IndexPath(row: /*ROW TO SCROLL TO*/, section: /*SECTION TO SCROLL TO*/)
tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)

修正了一些小错误。请注意,如果您确定您的
numberOfRowsInSection
始终返回1或更高的值,则可以删除while和下面的if语句。此解决方案需要一个按钮单击事件才能转到最后一个单元格。加载整个表格视图后,您可以使用带延迟的performSelector移动到最后一个单元格,无需单击按钮。您可以在**scrollToRowAtIndexPath**@amitgupta上调用
[self scrollToBottom:nil]
anywhere.app崩溃于gotoBottom方法,您可能试图将tableview滚动到不可用的索引,