美文网首页
UITableView 回到顶部

UITableView 回到顶部

作者: Yang152412 | 来源:发表于2017-10-27 11:29 被阅读31次

    一、tableView 的数据源没有变化

    这种情况最简单,有好几种方法

    • [tableView setContetnOffset:CGPointZero]
    • [tableView scrollToVisibleRect:]

    二、tableView 的数据源改变了

    比如增加或者删除数据,这时候 contentOffset 就不准了,所以上面的方法都不可用,可以用

    scrollToRowAtIndexPath 来实现。

    NSUInteger sections = [self.tableView numberOfSections];
        if (sections >= 1) {
            NSUInteger rows = [self.tableView numberOfRowsInSection:0];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
            if (rows == 0) {
                indexPath = [NSIndexPath indexPathForRow:NSNotFound inSection:0];
            }
            
            [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
        }
    

    相关文章

      网友评论

          本文标题:UITableView 回到顶部

          本文链接:https://www.haomeiwen.com/subject/zmsrpxtx.html