在使用MJRefresh情況下,tableVeiw跳轉(zhuǎn)到頂部mj_header偏移的問題及處理辦法

要想實(shí)現(xiàn)在reload之后彈出alertView,或者滾動(dòng)到特定一行, 也許你會(huì)這么寫

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

看似沒問題,但是滾動(dòng)沒起作用,因?yàn)閞eloadData是立即返回的,不會(huì)等tableview刷新完成。
解決辦法就是要等排隊(duì),等tableview的刷新操作完成,再去做滾動(dòng)等其他操作。
方法1:

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

這個(gè)方法官方的解釋有:
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]; 
});
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,299評(píng)論 3 38
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,406評(píng)論 4 61
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,878評(píng)論 2 45
  • (1月31日-2月9日,旅行11天。) “當(dāng)我們看到雪花翩然飄下,看到太陽從山后緩緩升起,看到一束光神秘縹緲地射進(jìn)...
    文心匠閱讀 1,387評(píng)論 14 10
  • 免費(fèi)的是最貴的,貴的是接收方,貴的是提供方,貴的才是價(jià)值最大化的。
    何鋒空間閱讀 230評(píng)論 0 0

友情鏈接更多精彩內(nèi)容