iOS UITableView的代理方法詳解

1、必須實現(xiàn)的回調(diào)方法

返回每個分區(qū)的行數(shù)

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

返回每一行的cell

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

2、可選實現(xiàn)的方法

返回分區(qū)數(shù)(默認為1)

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

返回每個分區(qū)頭部的標題

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

返回每個分區(qū)的尾部標題

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

設置某行是否可編輯

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

設置某行是否可以被移動

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

設置索引欄標題數(shù)組(實現(xiàn)這個方法尤辱,會在tableView右邊顯示每個分區(qū)的索引)

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

設置索引欄標題對應的分區(qū)

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

tableView接受編輯時調(diào)用的方法

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

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

?

1

2

3

4

5

typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {

UITableViewCellEditingStyleNone,//沒有編輯操作

UITableViewCellEditingStyleDelete,//刪除操作

UITableViewCellEditingStyleInsert//插入操作

};

tableView的cell被移動時調(diào)用的方法

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

三未蝌、UITableViewDelegate(tableView代理)

cell將要顯示時調(diào)用的方法

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

頭視圖將要顯示時調(diào)用的方法

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

尾視圖將要顯示時調(diào)用的方法

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

和上面的方法對應,這三個方法分別是cell茧妒,頭視圖萧吠,尾視圖已經(jīng)顯示時調(diào)用的方法

- (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高亮和取消高亮時分別調(diào)用的函數(shù)

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

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

當即將選中某行和取消選中某行時調(diào)用的函數(shù),返回一直位置,執(zhí)行選中或者取消選中

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

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

已經(jīng)選中和已經(jīng)取消選中后調(diào)用的函數(shù)

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

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

設置tableView被編輯時的狀態(tài)風格狰腌,如果不設置除破,默認都是刪除風格

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

自定義刪除按鈕的標題

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

下面這個方法是IOS8中的新方法,用于自定義創(chuàng)建tableView被編輯時右邊的按鈕琼腔,按鈕類型為UITableViewRowAction瑰枫。

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

設置編輯時背景是否縮進

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

將要編輯和結(jié)束編輯時調(diào)用的方法

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

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

移動特定的某行

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市丹莲,隨后出現(xiàn)的幾起案子光坝,更是在濱河造成了極大的恐慌,老刑警劉巖甥材,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件盯另,死亡現(xiàn)場離奇詭異,居然都是意外死亡洲赵,警方通過查閱死者的電腦和手機鸳惯,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來叠萍,“玉大人芝发,你說我怎么就攤上這事〖罅睿” “怎么了后德?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長抄腔。 經(jīng)常有香客問我瓢湃,道長,這世上最難降的妖魔是什么赫蛇? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任绵患,我火速辦了婚禮,結(jié)果婚禮上悟耘,老公的妹妹穿的比我還像新娘落蝙。我一直安慰自己,他們只是感情好暂幼,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布筏勒。 她就那樣靜靜地躺著,像睡著了一般旺嬉。 火紅的嫁衣襯著肌膚如雪管行。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天邪媳,我揣著相機與錄音捐顷,去河邊找鬼荡陷。 笑死,一個胖子當著我的面吹牛迅涮,可吹牛的內(nèi)容都是我干的废赞。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼叮姑,長吁一口氣:“原來是場噩夢啊……” “哼唉地!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起戏溺,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤渣蜗,失蹤者是張志新(化名)和其女友劉穎屠尊,沒想到半個月后旷祸,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡讼昆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年托享,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片浸赫。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡闰围,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出既峡,到底是詐尸還是另有隱情羡榴,我是刑警寧澤,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布运敢,位于F島的核電站校仑,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏传惠。R本人自食惡果不足惜迄沫,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望卦方。 院中可真熱鬧羊瘩,春花似錦、人聲如沸盼砍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽浇坐。三九已至睬捶,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間吗跋,已是汗流浹背侧戴。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工宁昭, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人酗宋。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓积仗,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蜕猫。 傳聞我的和親對象是個殘疾皇子寂曹,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容