要想實(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];
});