UITableView代理方法和執(zhí)行順序

一蚤霞、前言

iOS中UITableView是最常用的一個(gè)控件管宵】侔看了一下UITableView的代理:UITableViewDelegate 和 UITableViewDataSource桶良。其中UITableViewDelegate的方法有38個(gè)嗜侮,UITableViewDataSource的方法有11個(gè)焊虏。下面來簡單介紹一下淡喜。(方法代碼有點(diǎn)多)

二、UITableViewDelegate方法

UITableViewDelegate的38個(gè)方法所有的都是可選的诵闭。也就是你可以實(shí)現(xiàn)炼团,也可以不實(shí)現(xiàn)。這里就不一一介紹了疏尿,簡單的看一下這個(gè)圖:

代理方法

三瘟芝、UITableViewDataSource方法

UITableViewDataSource的11個(gè)方法中有2個(gè)是必須實(shí)現(xiàn)的:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

簡單看一下UITableViewDataSource代理方法:

UITableViewDatasource代理方法

四、方法執(zhí)行順序

這里面沒有將所有的方法都考慮進(jìn)去褥琐,這里只是對(duì)UITableView實(shí)例從創(chuàng)建到完全顯示進(jìn)行測試锌俱,而且都是常用的方法,實(shí)現(xiàn)其他方法其輸出內(nèi)容可能不一樣敌呈,但整體結(jié)構(gòu)不變贸宏。其中包括

UITableViewDelegate的:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@",NSStringFromSelector(_cmd));
}
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    NSLog(@"%@",NSStringFromSelector(_cmd));
}
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section {
    NSLog(@"%@",NSStringFromSelector(_cmd));
}
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
    NSLog(@"%@",NSStringFromSelector(_cmd));
}
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section {
    NSLog(@"%@",NSStringFromSelector(_cmd));
}
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section {
    NSLog(@"%@",NSStringFromSelector(_cmd));
}

// Variable height support

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    return 10;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    UILabel *label = [[UILabel alloc] init];
    label.text = @"頭標(biāo)題";
    return label;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    UILabel *label = [UILabel new];
    label.text = @"尾標(biāo)題";
    return label;
}

以及UITableViewDataSource的:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    NSLog(@"%@",NSStringFromSelector(_cmd));
  
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = self.contentArray[indexPath.row];
    NSLog(@"%@",NSStringFromSelector(_cmd));
    return cell;
}

//設(shè)置section的個(gè)數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   NSLog(@"----------------------------------------------------------------------%ld",++inde);
    NSLog(@"%@",NSStringFromSelector(_cmd));
    return 1;
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    return @"headerTitle";
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    return @"footerTitle";
}

這里我是講section得個(gè)數(shù)設(shè)置為1,每個(gè)section中的row為2磕洪】粤罚控制臺(tái)輸出結(jié)果如下:

2016-07-27 18:00:19.042 UITableViewMethodsTest[2584:839554] ----------------------------------------------------------------------1
2016-07-27 18:00:19.042 UITableViewMethodsTest[2584:839554] numberOfSectionsInTableView:
2016-07-27 18:00:19.042 UITableViewMethodsTest[2584:839554] tableView:heightForHeaderInSection:
2016-07-27 18:00:19.043 UITableViewMethodsTest[2584:839554] tableView:heightForHeaderInSection:
2016-07-27 18:00:19.043 UITableViewMethodsTest[2584:839554] tableView:heightForFooterInSection:
2016-07-27 18:00:19.043 UITableViewMethodsTest[2584:839554] tableView:heightForFooterInSection:
2016-07-27 18:00:19.043 UITableViewMethodsTest[2584:839554] tableView:numberOfRowsInSection:
2016-07-27 18:00:19.043 UITableViewMethodsTest[2584:839554] tableView:heightForRowAtIndexPath:
2016-07-27 18:00:19.043 UITableViewMethodsTest[2584:839554] tableView:heightForRowAtIndexPath:
2016-07-27 18:00:19.048 UITableViewMethodsTest[2584:839554] ----------------------------------------------------------------------2
2016-07-27 18:00:19.049 UITableViewMethodsTest[2584:839554] numberOfSectionsInTableView:
2016-07-27 18:00:19.049 UITableViewMethodsTest[2584:839554] tableView:heightForHeaderInSection:
2016-07-27 18:00:19.049 UITableViewMethodsTest[2584:839554] tableView:heightForHeaderInSection:
2016-07-27 18:00:19.049 UITableViewMethodsTest[2584:839554] tableView:heightForFooterInSection:
2016-07-27 18:00:19.049 UITableViewMethodsTest[2584:839554] tableView:heightForFooterInSection:
2016-07-27 18:00:19.049 UITableViewMethodsTest[2584:839554] tableView:numberOfRowsInSection:
2016-07-27 18:00:19.049 UITableViewMethodsTest[2584:839554] tableView:heightForRowAtIndexPath:
2016-07-27 18:00:19.049 UITableViewMethodsTest[2584:839554] tableView:heightForRowAtIndexPath:
2016-07-27 18:00:19.067 UITableViewMethodsTest[2584:839554] ----------------------------------------------------------------------3
2016-07-27 18:00:19.067 UITableViewMethodsTest[2584:839554] numberOfSectionsInTableView:
2016-07-27 18:00:19.067 UITableViewMethodsTest[2584:839554] tableView:heightForHeaderInSection:
2016-07-27 18:00:19.067 UITableViewMethodsTest[2584:839554] tableView:heightForHeaderInSection:
2016-07-27 18:00:19.067 UITableViewMethodsTest[2584:839554] tableView:heightForFooterInSection:
2016-07-27 18:00:19.067 UITableViewMethodsTest[2584:839554] tableView:heightForFooterInSection:
2016-07-27 18:00:19.067 UITableViewMethodsTest[2584:839554] tableView:numberOfRowsInSection:
2016-07-27 18:00:19.067 UITableViewMethodsTest[2584:839554] tableView:heightForRowAtIndexPath:
2016-07-27 18:00:19.067 UITableViewMethodsTest[2584:839554] tableView:heightForRowAtIndexPath:
2016-07-27 18:00:19.070 UITableViewMethodsTest[2584:839554] ----------------------------------------------------------------------4
2016-07-27 18:00:19.070 UITableViewMethodsTest[2584:839554] numberOfSectionsInTableView:
2016-07-27 18:00:19.070 UITableViewMethodsTest[2584:839554] tableView:heightForHeaderInSection:
2016-07-27 18:00:19.070 UITableViewMethodsTest[2584:839554] tableView:heightForHeaderInSection:
2016-07-27 18:00:19.070 UITableViewMethodsTest[2584:839554] tableView:heightForFooterInSection:
2016-07-27 18:00:19.071 UITableViewMethodsTest[2584:839554] tableView:heightForFooterInSection:
2016-07-27 18:00:19.071 UITableViewMethodsTest[2584:839554] tableView:numberOfRowsInSection:
2016-07-27 18:00:19.071 UITableViewMethodsTest[2584:839554] tableView:heightForRowAtIndexPath:
2016-07-27 18:00:19.071 UITableViewMethodsTest[2584:839554] tableView:heightForRowAtIndexPath:
2016-07-27 18:00:19.072 UITableViewMethodsTest[2584:839554] tableView:cellForRowAtIndexPath:
2016-07-27 18:00:19.073 UITableViewMethodsTest[2584:839554] tableView:heightForRowAtIndexPath:
2016-07-27 18:00:19.073 UITableViewMethodsTest[2584:839554] tableView:willDisplayCell:forRowAtIndexPath:
2016-07-27 18:00:19.073 UITableViewMethodsTest[2584:839554] tableView:cellForRowAtIndexPath:
2016-07-27 18:00:19.074 UITableViewMethodsTest[2584:839554] tableView:heightForRowAtIndexPath:
2016-07-27 18:00:19.074 UITableViewMethodsTest[2584:839554] tableView:willDisplayCell:forRowAtIndexPath:
2016-07-27 18:00:19.074 UITableViewMethodsTest[2584:839554] tableView:viewForHeaderInSection:
2016-07-27 18:00:19.074 UITableViewMethodsTest[2584:839554] tableView:willDisplayHeaderView:forSection:
2016-07-27 18:00:19.074 UITableViewMethodsTest[2584:839554] tableView:viewForFooterInSection:
2016-07-27 18:00:19.074 UITableViewMethodsTest[2584:839554] tableView:willDisplayFooterView:forSection:

從結(jié)果可知,當(dāng)我們?cè)O(shè)置為1個(gè)section和一個(gè)每個(gè)section的row為2的時(shí)候析显,顯示出來一個(gè)UITableView的實(shí)例需要4輪(我這里用numberOfSectionsInTableView:方法做了分割鲫咽,所以是4輪):

第一輪~第三輪都是一樣的:

1、先執(zhí)行numberOfSectionsInTableView:一次

2叫榕、然后執(zhí)行tableView:heightForHeaderInSection:兩次

3浑侥、再執(zhí)行tableView:heightForFooterInSection:兩次

4、再執(zhí)行tableView:numberOfRowsInSection:一次

5晰绎、最后執(zhí)行tableView:heightForRowAtIndexPath:兩次

第四輪是這樣的:

1寓落、執(zhí)行numberOfSectionsInTableView:一次

2、執(zhí)行tableView:heightForHeaderInSection:兩次

3荞下、執(zhí)行tableView:heightForFooterInSection:兩次

4伶选、執(zhí)行tableView:numberOfRowsInSection:一次

5、執(zhí)行tableView:heightForRowAtIndexPath:兩次

前5步和前三輪是一樣的

6尖昏、執(zhí)行tableView:cellForRowAtIndexPath:一次

7仰税、執(zhí)行tableView:heightForRowAtIndexPath:一次

8、執(zhí)行tableView:willDisplayCell:forRowAtIndexPath:一次

9抽诉、10陨簇、11是重復(fù)6、7迹淌、8河绽。(因?yàn)槭莾蓚€(gè)row)

12己单、執(zhí)行tableView:viewForHeaderInSection:一次

13、執(zhí)行tableView:willDisplayHeaderView:forSection:一次

14耙饰、執(zhí)行tableView:viewForFooterInSection:一次

15纹笼、執(zhí)行tableView:willDisplayFooterView:forSection:一次

五、注意點(diǎn)

1苟跪、如果在設(shè)置header和footer信息的時(shí)候廷痘,View的優(yōu)先級(jí)高于titile。(Views are preferred over title should you decide to provide both)
  2件已、如果想讓header的title或者footer的title顯示不一樣的樣式(顏色笋额,字體大小)拨齐,可以用自定義view(UILabel)來實(shí)現(xiàn)鳞陨。
  3、注意復(fù)用單元格瞻惋。  
  4厦滤、設(shè)置headerTitle樣式的時(shí)候如果這樣寫:

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    static NSString *HEADTITLE = @"headerTitle";
    UITableViewHeaderFooterView *titleView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HEADTITLE];
    if (titleView == nil) {
        titleView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HEADTITLE];
    }
    titleView.textLabel.textColor = [UIColor redColor];
   titleView.textLabel.text  =@"頭部標(biāo)題";
    return titleView;
}

其中的textColor設(shè)置無效。此時(shí)可以在這里設(shè)置:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    UITableViewHeaderFooterView *vi = (UITableViewHeaderFooterView *)view;
    vi.textLabel.textColor = [UIColor redColor];
    
    NSLog(@"%@",NSStringFromSelector(_cmd));
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末歼狼,一起剝皮案震驚了整個(gè)濱河市掏导,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌羽峰,老刑警劉巖趟咆,帶你破解...
    沈念sama閱讀 216,997評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異梅屉,居然都是意外死亡值纱,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門坯汤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來虐唠,“玉大人,你說我怎么就攤上這事惰聂〗ィ” “怎么了?”我有些...
    開封第一講書人閱讀 163,359評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵搓幌,是天一觀的道長杆故。 經(jīng)常有香客問我,道長溉愁,這世上最難降的妖魔是什么处铛? 我笑而不...
    開封第一講書人閱讀 58,309評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上撤蟆,老公的妹妹穿的比我還像新娘篙贸。我一直安慰自己,他們只是感情好枫疆,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評(píng)論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著敷鸦,像睡著了一般息楔。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上扒披,一...
    開封第一講書人閱讀 51,258評(píng)論 1 300
  • 那天值依,我揣著相機(jī)與錄音,去河邊找鬼碟案。 笑死愿险,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的价说。 我是一名探鬼主播辆亏,決...
    沈念sama閱讀 40,122評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼鳖目!你這毒婦竟也來了扮叨?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,970評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤领迈,失蹤者是張志新(化名)和其女友劉穎彻磁,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體狸捅,經(jīng)...
    沈念sama閱讀 45,403評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡衷蜓,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了尘喝。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片磁浇。...
    茶點(diǎn)故事閱讀 39,769評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖瞧省,靈堂內(nèi)的尸體忽然破棺而出扯夭,到底是詐尸還是另有隱情,我是刑警寧澤鞍匾,帶...
    沈念sama閱讀 35,464評(píng)論 5 344
  • 正文 年R本政府宣布交洗,位于F島的核電站,受9級(jí)特大地震影響橡淑,放射性物質(zhì)發(fā)生泄漏构拳。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評(píng)論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望置森。 院中可真熱鬧斗埂,春花似錦、人聲如沸凫海。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽行贪。三九已至漾稀,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間建瘫,已是汗流浹背崭捍。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留啰脚,地道東北人殷蛇。 一個(gè)月前我還...
    沈念sama閱讀 47,831評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像橄浓,于是被迫代替她去往敵國和親粒梦。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評(píng)論 2 354

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