iOS開發- tableView的協議
在使用 UITableView 時,必須實現的協議主要包括以下幾個
1. UITableViewDataSource 協議
這是最重要的協議,用于提供數據給 UITableView。沒有這個協議,UITableView 是無法顯示任何內容的。
必須實現的方法:
-
tableView:numberOfRowsInSection::返回給定 section 中的行數。- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; -
tableView:cellForRowAtIndexPath::返回對應indexPath的單元格(UITableViewCell)。- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
這兩個方法是 UITableViewDataSource 協議中最核心的必須實現的方法。
可選的方法:
-
tableView:titleForHeaderInSection::返回指定 section 的標題(用于表頭)。- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; -
tableView:titleForFooterInSection::返回指定 section 的標題(用于表尾)。- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section; -
tableView:canEditRowAtIndexPath::指示是否允許編輯某一行。- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; -
tableView:canMoveRowAtIndexPath::指示是否允許移動某一行。- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
2. UITableViewDelegate 協議
UITableViewDelegate 協議用于處理表視圖的交互,例如行選擇、行刪除、行移動等。這個協議的實現通常是為了增強用戶體驗。
必須實現的方法:
實際上,UITableViewDelegate 中并沒有嚴格“必須”實現的方法,但是通常會實現以下幾種常見方法:
-
tableView:didSelectRowAtIndexPath::當用戶點擊某一行時調用。- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
可選的方法:
-
tableView:heightForRowAtIndexPath::設置行高。- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; -
tableView:heightForHeaderInSection::設置表頭的高度。- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; -
tableView:heightForFooterInSection::設置表尾的高度。- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section; -
tableView:viewForHeaderInSection::自定義表頭視圖。- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; -
tableView:viewForFooterInSection::自定義表尾視圖。- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; -
tableView:didDeselectRowAtIndexPath::當用戶取消選擇某一行時調用。- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
3. UITableViewDragDelegate 和 UITableViewDropDelegate(iOS 11 及以上)
這些協議主要用于拖放操作(drag and drop)功能,適用于需要支持拖動排序或拖拽添加數據的表格。
UITableViewDragDelegate:用于處理行拖拽操作。UITableViewDropDelegate:用于處理行的接收(drop)操作。
這些協議方法在使用拖放功能時非常有用,但它們是可選的,只在支持拖放操作時才需要實現。
4. UITableViewDataSourcePrefetching(iOS 10 及以上)
如果表格需要進行數據預加載,UITableViewDataSourcePrefetching 協議非常有用。這個協議允許提前加載即將顯示的行的數據(例如,提前加載圖片或遠程數據)。
-
tableView:prefetchRowsAtIndexPaths::預加載數據的方法。- (void)tableView:(UITableView *)tableView prefetchRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths; -
tableView:cancelPrefetchingForRowsAtIndexPaths::取消預加載的數據的方法。- (void)tableView:(UITableView *)tableView cancelPrefetchingForRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;
總結
-
必需的協議:
UITableViewDataSource:主要負責提供數據。UITableViewDelegate:主要負責處理交互(例如行的選擇、編輯、行高等)。
-
可選的協議:
UITableViewDragDelegate和UITableViewDropDelegate(用于拖放操作)。UITableViewDataSourcePrefetching(用于數據預加載)。
大部分時候,只需要實現 UITableViewDataSource 和 UITableViewDelegate 中的幾個關鍵方法。如果還需要自定義其他功能(例如拖放、數據預加載),可以根據需求再實現其他協議的方法。
而使用UIcollectionView也是相同的。

浙公網安備 33010602011771號