下拉刷新是列表頁常見互動。可透過監聽 scrollView 的 contentOffset,或使用第三方套件完成。
若自行實作,可在 willDisplayCell 中判斷是否已滾動到最後一筆,再顯示 loading 指示器並在背景佇列載入資料。
if (indexPath.row == self.dataArray.count - 1) {
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(130, 0, 60, 60)];
[activity startAnimating];
[footer addSubview:activity];
self.tableView.tableFooterView = footer;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 模擬載入資料
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
}
實作時需注意:更新 UI 必須回到主執行緒,且要避免重複觸發載入。