UITableView代理方法詳解

一开伏、補充

在上一篇簡書中遭商,http://www.reibang.com/p/1a3533d8a258劫流,我將IOS中tableView(表視圖)的一些常用方法總結(jié)了一下,這篇將tableView的代理方法作了總結(jié)仍秤,對上一篇簡書進行了補充可很。

二、UITableViewDataSourc(數(shù)據(jù)源代理)

1苇本、必須實現(xiàn)的回調(diào)方法
返回每個分區(qū)的行數(shù)

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

返回每一行的cell

- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath;

2菜拓、可選實現(xiàn)的方法
返回分區(qū)數(shù)(默認為1)

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

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

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

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

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

設(shè)置某行是否可編輯

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

設(shè)置某行是否可以被移動

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

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

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

設(shè)置索引欄標(biāo)題對應(yīng)的分區(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被編輯的模式,如下:
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;

和上面的方法對應(yīng)氯迂,這三個方法分別是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;

設(shè)置行高弄捕,頭視圖高度和尾視圖高度的方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

設(shè)置行高导帝,頭視圖高度和尾視圖高度的估計值(對于高度可變的情況下您单,提高效率)

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section;

設(shè)置自定義頭視圖和尾視圖

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

設(shè)置cell是否可以高亮

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

cell高亮和取消高亮?xí)r分別調(diào)用的函數(shù)

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

當(dāng)即將選中某行和取消選中某行時調(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;

設(shè)置tableView被編輯時的狀態(tài)風(fēng)格蜈彼,如果不設(shè)置俺驶,默認都是刪除風(fēng)格

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

自定義刪除按鈕的標(biāo)題

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

設(shè)置編輯時背景是否縮進

- (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,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件帅霜,死亡現(xiàn)場離奇詭異身冀,居然都是意外死亡括享,警方通過查閱死者的電腦和手機珍促,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進店門猪叙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人犬第,你說我怎么就攤上這事藏否。” “怎么了遥椿?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵冠场,是天一觀的道長本砰。 經(jīng)常有香客問我,道長舔株,這世上最難降的妖魔是什么还棱? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任珍手,我火速辦了婚禮,結(jié)果婚禮上寡具,老公的妹妹穿的比我還像新娘稚补。我一直安慰自己,他們只是感情好孔厉,可當(dāng)我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著拼余,像睡著了一般亩歹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上亭姥,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天达罗,我揣著相機與錄音静秆,去河邊找鬼。 笑死扶认,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的殊橙。 我是一名探鬼主播辐宾,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼膨蛮!你這毒婦竟也來了叠纹?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤敞葛,失蹤者是張志新(化名)和其女友劉穎吊洼,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體制肮,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年递沪,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片款慨。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡儒飒,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出檩奠,到底是詐尸還是另有隱情桩了,我是刑警寧澤附帽,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站井誉,受9級特大地震影響蕉扮,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜颗圣,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一喳钟、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧在岂,春花似錦奔则、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至及老,卻和暖如春抽莱,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背写半。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工岸蜗, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人叠蝇。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓璃岳,卻偏偏與公主長得像,于是被迫代替她去往敵國和親悔捶。 傳聞我的和親對象是個殘疾皇子铃慷,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,047評論 2 355

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