萬盛學電腦網

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

iOS UITableView 核心協議與數據源方法速查表

必須遵循的兩大協議


@interface MyViewController : UIViewController 
@end

DataSource 必須實現(2 個)


// 1. 行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

// 2. Cell 配置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    Model *item = self.dataArray[indexPath.row];
    cell.textLabel.text = item.title;
    cell.detailTextLabel.text = item.subtitle;
    cell.imageView.image = [UIImage imageNamed:item.icon];
    
    return cell;
}

DataSource 可選常用


// 分區數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }

// 分區標題
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return @"Section"; }
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { return nil; }

// 索引列
- (nullable NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return @[@"A",@"B",@"C"]; }
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { return index; }

// 編輯/移動
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; }
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; }
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { ... }
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)source toIndexPath:(NSIndexPath *)dest { ... }

Delegate 常用方法


// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 60; }
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { return 60; }

// 頭尾視圖高度/視圖
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 40; }
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { return customHeaderView; }

// 選中/取消選中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self performSegueWithIdentifier:@"Detail" sender:self.dataArray[indexPath.row]];
}

// 即將顯示/結束顯示(預加載/釋放資源)
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { ... }
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { ... }

// 編輯樣式/縮進/配件
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; }
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath { return YES; }

現代化:UITableViewDiffableDataSource (iOS 13+)


// 類型安全、自動動畫、無需手動 indexPath 計算
typealias DataSource = UITableViewDiffableDataSource
typealias Snapshot = NSDiffableDataSourceSnapshot

lazy var dataSource = DataSource(tableView: tableView) { tableView, indexPath, item in
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.textLabel?.text = item.title
    return cell
}

func applySnapshot(items: [Item], animating: Bool = true) {
    var snapshot = Snapshot()
    snapshot.appendSections([.main])
    snapshot.appendItems(items, toSection: .main)
    dataSource.apply(snapshot, animatingDifferences: animating)
}

性能優化清單

  • ✅ 復用 Cell:dequeueReusableCellWithIdentifier:
  • ✅ 預估行高:estimatedHeightForRowAtIndexPath: 避免佈局抖動
  • ✅ 異步加載圖片:SDWebImage / Kingfisher / Nuke
  • ✅ 避免在 cellForRow 做重計算:預處理模型數據
  • ✅ 批量更新:performBatchUpdates: 或 DiffableDataSource
  • ✅ 取消離屏 Cell 的網絡請求:didEndDisplayingCell 中 cancel
copyright © 萬盛學電腦網 all rights reserved