方法一 滚动到区域
[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];
网友评论