在使用MJRefresh情况下,tableVeiw跳转到顶部mj_header偏移的问题及处理办法

要想实现在reload之后弹出alertView,或者滚动到特定一行, 也许你会这么写

[_tableView reloadData];
[_tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated];

看似没问题,但是滚动没起作用,因为reloadData是立即返回的,不会等tableview刷新完成
解决办法就是要等排队,等tableview的刷新操作完成,再去做滚动等其他操作。
方法1:

  - (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay;

这个方法官方的解释有:
Specifying a delay of 0 does not necessarily cause the selector to be performed immediately. The selector is still queued on the thread’s run loop and performed as soon as possible.

正好符合咱们的要求。

- (void)reload{ 
    [_tableView reloadData];
    [self performSelector:@selector(scrollToIndexPath:) withObject:[NSIndexPath indexPathForRow:rowIndex inSection:sectionIndex] afterDelay:0.0];
}
- (void)scrollToIndexPath:(NSIndexPath *)path{
   [_tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

方法2:

  [_tableView reloadData];
  dispatch_async(dispatch_get_main_queue(), ^{
   [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rowIndex inSection:sectionIndex] atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
});
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,984评论 3 38
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,655评论 4 61
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,757评论 2 45
  • (1月31日-2月9日,旅行11天。) “当我们看到雪花翩然飘下,看到太阳从山后缓缓升起,看到一束光神秘缥缈地射进...
    文心匠阅读 5,092评论 14 10
  • 免费的是最贵的,贵的是接收方,贵的是提供方,贵的才是价值最大化的。
    何锋空间阅读 1,562评论 0 0

友情链接更多精彩内容