iOS UITableView代理方法詳解

來源:互聯網
上載者:User

標籤:

 

原 iOS UITableView代理方法詳解IOS UITableView的代理方法詳解(http://my.oschina.net/u/2340880/blog/404958)一、補充

在上一篇部落格中,http://my.oschina.net/u/2340880/blog/404605,我將IOS中tableView(表視圖)的一些常用方法總結了一下,這篇將tableView的代理方法作了總結,對上一篇部落格進行了補充。

二、UITableViewDataSourc(資料來源代理)1、必須實現的回調方法

返回每個分區的行數

 

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

 

返回每一行的cell

 

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

 

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被編輯的模式,如下:

?
12345 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;  

 

疏漏之處 歡迎指正

學習使用 歡迎轉載

 

iOS UITableView代理方法詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.