天天看點

iOS UITableView代理方法詳解iOS UITableView的代理方法詳解

iOS UITableView的代理方法詳解

一、補充

在上一篇部落格中,

http://my.oschina.net/u/2340880/blog/404605

,我将IOS中tableView(表視圖)的一些常用方法總結了一下,這篇将tableView的代理方法作了總結,對上一篇部落格進行了補充。

二、UITableViewDataSourc(資料源代理)

1、必須實作的回調方法

傳回每個分區的行數

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

傳回每一行的cell

2、可選實作的方法

傳回分區數(預設為1)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;  

傳回每個分區頭部的标題

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

傳回每個分區的尾部标題

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

設定某行是否可編輯

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

設定某行是否可以被移動

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

設定索引欄标題數組(實作這個方法,會在tableView右邊顯示每個分區的索引)

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;

設定索引欄标題對應的分區

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index

tableView接受編輯時調用的方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

這個方法中的editingStyle參數是一個枚舉,代表了cell被編輯的模式,如下:

typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {

   UITableViewCellEditingStyleNone,//沒有編輯操作

   UITableViewCellEditingStyleDelete,//删除操作

   UITableViewCellEditingStyleInsert//插入操作

};

tableView的cell被移動時調用的方法

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

三、UITableViewDelegate(tableView代理)

cell将要顯示時調用的方法

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

頭視圖将要顯示時調用的方法

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section;

尾視圖将要顯示時調用的方法

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section;

和上面的方法對應,這三個方法分别是cell,頭視圖,尾視圖已經顯示時調用的方法

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath;

- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section;

- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section;

設定行高,頭視圖高度和尾視圖高度的方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

設定行高,頭視圖高度和尾視圖高度的估計值(對于高度可變的情況下,提高效率)

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section;

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section;

設定自定義頭視圖和尾視圖

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

設定cell是否可以高亮

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath;

cell高亮和取消高亮時分别調用的函數

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath;

當即将選中某行和取消選中某行時調用的函數,傳回一直位置,執行選中或者取消選中

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;

- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath;

已經選中和已經取消選中後調用的函數

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;

設定tableView被編輯時的狀态風格,如果不設定,預設都是删除風格

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;

自定義删除按鈕的标題

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath;

下面這個方法是IOS8中的新方法,用于自定義建立tableView被編輯時右邊的按鈕,按鈕類型為UITableViewRowAction。

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath ;

設定編輯時背景是否縮進

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;

将要編輯和結束編輯時調用的方法

- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath;

移動特定的某行

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;