UItableView滾動到特定位置
阿新 • • 發佈:2017-09-27
執行 如果 for 直接 ima scroll 方法 lsp ger
如果在reloadData後需要立即獲取tableview的cell、高度,或者需要滾動tableview,那麽,直接在reloadData後執行代碼是有可能出問題的。
reloadDate並不會等待tableview更新結束後才返回,而是立即返回,然後去計算表高度,獲取cell等。
如果表中的數據非常大,在一個run loop周期沒執行完,這時,需要tableview視圖數據的操作就會出問題了。
apple並沒有直接提供reloadData的api,想要程序延遲到reloadData結束在操作,可以用以下方法:
方法一:
1 2 3 |
[self.tableView reloadData]; [self.tableView layoutIfNeeded];
//刷新完成
|
方法二:
1 2 3 4 |
[self.tableView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
//刷新完成
});
|
註意上面的。下面為刷新後,滾動代碼(滾動到底)
//刷新完成 NSInteger s = [chatTableView numberOfSections]; if (s<1) return; NSInteger r = [chatTableView numberOfRowsInSection:s-1]; if (r<1) return; NSIndexPath *ip = [NSIndexPath indexPathForRow:r-1 inSection:s-1]; [chatTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:animated];
方法二: [chatTableView setContentOffset:CGPointMake(0, CGFLOAT_MAX)];
UItableView滾動到特定位置