一蚤霞、前言
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代理方法:
四、方法執(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));
}