您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 使用技巧 >> iOS 技巧大全

UITableView 下拉刷新實作

編輯:iOS 技巧大全

下拉刷新是列表頁常見互動。可透過監聽 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 必須回到主執行緒,且要避免重複觸發載入。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved