萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> iOS 開發

iOS UITableView 局部刷新:精準更新單行或多行數據

reloadData 會重載整個表格,導致滾動位置重置、動畫閃爍、性能開銷大。針對單行/多行變更,應使用 reloadRowsAtIndexPaths:withRowAnimation: 實現精準局部更新。

核心 API

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths 
             withRowAnimation:(UITableViewRowAnimation)animation;

單行刷新示例

- (void)updateRowAtIndex:(NSInteger)row inSection:(NSInteger)section {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] 
                          withRowAnimation:UITableViewRowAnimationAutomatic];
}

多行批量刷新

- (void)updateRowsAtIndexes:(NSIndexSet *)indexSet inSection:(NSInteger)section {
    NSMutableArray *indexPaths = [NSMutableArray array];
    [indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
        [indexPaths addObject:[NSIndexPath indexPathForRow:idx inSection:section]];
    }];
    [self.tableView reloadRowsAtIndexPaths:indexPaths 
                          withRowAnimation:UITableViewRowAnimationFade];
}

動畫類型選擇指南

常量效果推薦場景
UITableViewRowAnimationAutomatic系統自動選擇(通常為 Fade)通用、無特殊要求
UITableViewRowAnimationFade淡入淡出數據內容替換、狀態切換
UITableViewRowAnimationRight/Left從右/左滑入側邊欄展開、抽屉效果
UITableViewRowAnimationTop/Bottom從頂/底滑入列表頂部/底部插入
UITableViewRowAnimationMiddle從中間擴展/收縮分組展開折疊
UITableViewRowAnimationNone無動畫高頻更新、性能敏感

進階:帶模型對比的智能刷新

- (void)updateItems:(NSArray *)newItems {
    NSMutableArray *indexPathsToReload = [NSMutableArray array];
    
    for (NSUInteger i = 0; i < newItems.count; i++) {
        Item *newItem = newItems[i];
        Item *oldItem = self.dataSource[i];
        
        // 業務邏輯判斷是否真正變化
        if (![newItem isEqual:oldItem]) {
            self.dataSource[i] = newItem;
            [indexPathsToReload addObject:[NSIndexPath indexPathForRow:i inSection:0]];
        }
    }
    
    if (indexPathsToReload.count > 0) {
        [self.tableView reloadRowsAtIndexPaths:indexPathsToReload 
                              withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

配套操作:插入/刪除/移動

// 插入
[self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationRight];

// 刪除
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

// 移動
[self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];

批量更新事務(iOS 11+)

[self.tableView performBatchUpdates:^{
    [self.tableView reloadRowsAtIndexPaths:reloadPaths withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView insertRowsAtIndexPaths:insertPaths withRowAnimation:UITableViewRowAnimationRight];
    [self.tableView deleteRowsAtIndexPaths:deletePaths withRowAnimation:UITableViewRowAnimationLeft];
} completion:^(BOOL finished) {
    // 完成回調
}];

避坑指南

  • ❌ 在 reloadRowsAtIndexPaths 前未更新 dataSource → 崩潰或顯示舊數據。
  • ❌ IndexPath 越界(section/row 不存在) → Crash。
  • ❌ 動畫類型與操作語義不符(如刪除用 Top) → 視覺困惑。
  • ✅ 始終先更新數據模型,再調用刷新方法。
  • ✅ 使用 performBatchUpdates 合併多操作,保證動畫同步。

精準局部刷新,是列表流暢體驗與性能優化的關鍵技能。

copyright © 萬盛學電腦網 all rights reserved