今天跟大家分享一下UITableView的各個代理方法的用法,主要是根據SDK裏面的介紹,再加上自己的分析與測試,總結成此文。如果有疑問的話,希望大家留言告訴我~
本文主要講解對於UITableView最重要的兩個協議
UITableViewDataSource
UITableViewDelegate
UITableViewDataSource
1)tableView:cellForRowAtIndexPath:
申請一個cell插入到表視圖特定的位置,cell生成訪問到的方法。
2)numberOfSectionsInTableView:
返回表視圖的分區數。
3)tableView:numberOfRowsInSection:
返回表視圖中每個分區的行數。
4)sectionIndexTitlesForTableView:
返回分區索引的名稱,使用此方法會在表視圖右側創建一個索引欄,通過點擊索引可以快速跳轉到指定分區。
5)tableView:sectionForSectionIndexTitle:atIndex:
點擊索引欄會調用此事件,通過點擊的標題與索引返回分區的索引。簡單來說,就是設定點擊右側索引欄會跳轉到的分區,如return 0,那麼無論點擊什麼,都會跳轉到分區0。
6)tableView:titleForHeaderInSection:
定義每一個分區頭的名稱。
7)tableView:titleForFooterInSection:
定義每一個分區尾的名稱。
8)tableView:commitEditingStyle:forRowAtIndexPath:
要求數據源提交插入或者刪除指定行的事件。即每次刪除或者插入完成後都會響應該方法,commitEditingStyle參數標識此次操作是UITableViewCellEditingStyleInsert(插入)
or UITableViewCellEditingStyleDelete(刪除)。
9)tableView:canEditRowAtIndexPath:
使得指定行爲可編輯狀態。
10)tableView:canMoveRowAtIndexPath:
使得指定行成可移動狀態,如果指定爲NO,則該行不會出現可拖動圖標。
11)tableView:moveRowAtIndexPath:toIndexPath:
移動行之後調用的方法,可以在裏面設置表視圖數據list的一些操作。
UITableViewDelegate
1)tableView:heightForRowAtIndexPath:
返回行的高度。
2)tableView:willDisplayCell:forRowAtIndexPath:
在Cell將要顯示時調用此方法,可以在這裏修改重用的Cell的一些屬性,例如顏色。
3)tableView:willSelectRowAtIndexPath:
通知委託指定行被將要選中,返回值爲NSIndexPath是指返回響應行的索引,即可以點擊一行而返回另一行索引,如果不想點擊事件響應則返回nil。
4)tableView:didSelectRowAtIndexPath:
通知委託指定行被選中。
5)tableView:willDeselectRowAtIndexPath:
通知委託指定行將取消選中。
6)tableView:didDeselectRowAtIndexPath:
通知委託指定行被取消選中。
關於3)到6)的執行順序:
willSelectRowAtIndexPath 當前行爲:0
didSelectRowAtIndexPath 當前行爲:0
willSelectRowAtIndexPath 當前行爲:1
willDeselectRowAtIndexPath 當前行爲:0
didDeselectRowAtIndexPath 當前行爲:0
didSelectRowAtIndexPath 當前行爲:1
注:在下一行將要選中後才取消上一行的選中。
7)tableView:viewForHeaderInSection:
設置分區的header的視圖,可以爲它添加圖片或者其他控件(UIView的子類)
8)tableView:viewForFooterInSection:
設置分區footer的視圖,可以爲它添加圖片或者其他控件(UIView的子類)
9)tableView:heightForHeaderInSection:
設置分區header的高度。
10)tableView:heightForFooterInSection:
設置分區footer的高度
11)tableView:willDisplayHeaderView:forSection:
通知委託將要顯示分區header的視圖
12)tableView:willDisplayFooterView:forSection:
通知委託將要顯示分區footer的視圖
13)tableView:willBeginEditingRowAtIndexPath:
通知委託,表視圖將要被編輯(刪除或者插入,而是不是編輯cell文字哦)
14)tableView:didEndEditingRowAtIndexPath:
表視圖完成編輯。
15)tableView:editingStyleForRowAtIndexPath:
對當前行設置編輯模式,刪除、插入或者不可編輯。
typedef enum {
//不可編輯
UITableViewCellEditingStyleNone,
//刪除
UITableViewCellEditingStyleDelete,
//插入
UITableViewCellEditingStyleInsert
}UITableViewCellEditingStyle;
16)tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:
在刪除模式啓動下,改變每行刪除按鈕的文字(默認爲“Delete”)
17)tableView:shouldIndentWhileEditingRowAtIndexPath:
通知委託在編輯模式下是否需要對錶視圖指定行進行縮進,NO爲關閉縮進,這個方法可以用來去掉move時row前面的空白。
18)tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:
移動行的過程中會多次調用此方法,返回值代表進行移動操作後回到的行,如果設置爲當前行,則不論怎麼移動都會回到當前行。
19)tableView:didEndDisplayingCell:forRowAtIndexPath:
通知委託指定的cell已經移出表視圖,即拖動的時候移出視圖的cell。
20)tableView:didEndDisplayingHeaderView:forSection:
通知委託指定的分區頭已經移出表視圖。21)tableView:didEndDisplayingFooterView:forSection:
通知委託指定的分區尾已經移出視圖
22)tableView:shouldShowMenuForRowAtIndexPath:
通知委託是否在指定行顯示菜單,返回值爲YES時,長按顯示菜單。
23)tableView:canPerformAction:forRowAtIndexPath:withSender:
彈出選擇菜單時會調用此方法(複製、粘貼、全選、剪切)
24)tableView:performAction:forRowAtIndexPath:withSender:
選擇菜單項完成之後調用此方法。
25)tableView:shouldHighlightRowAtIndexPath:
通知委託是否開啓點擊高亮顯示,YES爲顯示。
26)tableView:didHighlightRowAtIndexPath:
通知委託表示圖的指定行被高亮顯示。
27)tableView:didUnhighlightRowAtIndexPath:
通知委託表視圖的指定行不在高亮顯示,一般是點擊其他行的時候。