0
点赞
收藏
分享

微信扫一扫

iOS小技能: tableView滚到指定位置(视图的最顶部或者最底部)

前言

需求: 显示一个 top 的按钮 ,点top返回到顶部

应用场景1: 界面内容比较多的时候,需要一个返回到顶部的开关

应用场景2·:当一个界面的数据更新之后,我们希望回到顶部的时候就可以使用这个方法。

例子: 当计算表达式发生变化时,需要将收银台界面的计算器界面回到最顶部进行显示最新的计算结果

iOS小技能: tableView滚到指定位置(视图的最顶部或者最底部)_公众号_02

I、iOS tableView滚到最顶部的方案

1.1 scrollToRowAtIndexPath

​本人比较喜欢采用``scrollToRowAtIndexPath​​,因为它不仅可以滚动到最顶部,还支持到特定位置。

点top返回到顶部

NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

滚动到指定位置

NSIndexPath indexPathForRow:QCTChangeBasicViewSection4businessLicenseInforow4Last inSection:QCTChangeBasicViewSection4businessLicenseInfo] atScrollPosition:UITableViewScrollPositionBottom animated:YES];

1.2 setContentOffset

  • setContentOffset

CGPointMake(0,0) animated:NO];

1.3 scrollRectToVisible

  • scrollRectToVisible

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

1.4 scrollsToTop

[weakSelf.tableView scrollsToTop];

UIScrollViewDelegate 的scrollViewShouldScrollToTop 方法必须返回YES

iOS小技能: tableView滚到指定位置(视图的最顶部或者最底部)_小程序_03

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;   // return a yes if you want to scroll to the top. if not defined, assumes YES

II scrollToRowAtIndexPath实现特定需求

使用scrollToRowAtIndexPath来解决更新reloadData的带来的界面跳动

更新公告分组数据时就可以使用以下方式来解决

self.viewModel.reloadSubject4Note subscribeNext:^(id  _Nullable x) {


if(self.viewModel.isShow_msg){
[weakSelf.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:QCTStoreViewSection4msg] atScrollPosition:UITableViewScrollPositionTop animated:YES];


}else{


[weakSelf.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:QCTStoreViewSection4Banner] atScrollPosition:UITableViewScrollPositionTop animated:YES];


// :

iOS小技能: tableView滚到指定位置(视图的最顶部或者最底部)_ios_04

see also

更多内容请关注​​#小程序:iOS逆向​​,只为你呈现有价值的信息,专注于移动端技术研究领域。

举报

相关推荐

0 条评论