美文网首页
TableView、CollectionView回到顶部全解

TableView、CollectionView回到顶部全解

作者: 择势量投 | 来源:发表于2018-09-13 16:26 被阅读241次

方法一 滚动到区域

[self.tableView  scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];  

方法二 设置偏移量

[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; 

方法三 滚动到指定的cell,无sectionHeader 回到顶部

  //!< 说明 section不能为NSNotFound row可以为NSNotFound,避免无数据时,引起崩溃😖
  if (_tableView.numberOfSections) {
            [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:NSNotFound inSection:0] atScrollPosition: UITableViewScrollPositionTop animated:NO];
        }

tableView、collectionView 有刷新动作时,有可能仅使用上面的一个方法会有无效的情况,建议使用方法四,方法三和方法二的组合

方法四 滚动到指定的cell,有sectionHeader 再回到顶部

  //!< 说明 section不能为NSNotFound row可以为NSNotFound,避免无数据时,引起崩溃😖
  if (_tableView.numberOfSections) {
            [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:NSNotFound inSection:0] atScrollPosition: UITableViewScrollPositionTop animated:NO];
        }
[self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; 
        

相关文章

网友评论

      本文标题:TableView、CollectionView回到顶部全解

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