一、前言
iOS中UITableView是最常用的一個控件。看了一下UITableView的代理:UITableViewDelegate 和 UITableViewDataSource。其中UITableViewDelegate的方法有38個,UITableViewDataSource的方法有11個。下面來簡單介紹一下。(方法代碼有點多)
二、UITableViewDelegate方法
UITableViewDelegate的38個方法所有的都是可選的。也就是你可以實現,也可以不實現。這里就不一一介紹了,簡單的看一下這個圖:
三、UITableViewDataSource方法
UITableViewDataSource的11個方法中有2個是必須實現的:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
簡單看一下UITableViewDataSource代理方法:
五、方法執行順序問題
這里面沒有將所有的方法都考慮進去,這里只是對UITableView實例從創建到完全顯示進行測試,而且都是常用的方法,實現其他方法其輸出內容可能不一樣,但整體結構不變。其中包括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 = @"頭標題"; return label; } - (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { NSLog(@"%@",NSStringFromSelector(_cmd)); UILabel *label = [UILabel new]; label.text = @"尾標題"; 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; } //設置section的個數 - (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得個數設置為1,每個section中的row為2。控制台輸出結果如下:
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:
從結果可知,當我們設置為1個section和一個每個section的row為2的時候,顯示出來一個UITableView的實例需要4輪(我這里用numberOfSectionsInTableView:方法做了分割,所以是4輪):
第一輪~第三輪都是一樣的:
1、先執行numberOfSectionsInTableView:一次
2、然后執行tableView:heightForHeaderInSection:兩次
3、再執行tableView:heightForFooterInSection:兩次
4、再執行tableView:numberOfRowsInSection:一次
5、最后執行tableView:heightForRowAtIndexPath:兩次
第四輪是這樣的:
1、執行numberOfSectionsInTableView:一次
2、執行tableView:heightForHeaderInSection:兩次
3、執行tableView:heightForFooterInSection:兩次
4、執行tableView:numberOfRowsInSection:一次
5、執行tableView:heightForRowAtIndexPath:兩次
前5步和前三輪是一樣的
6、執行tableView:cellForRowAtIndexPath:一次
7、執行tableView:heightForRowAtIndexPath:一次
8、執行tableView:willDisplayCell:forRowAtIndexPath:一次
9、10、11是重復6、7、8。(因為是兩個row)
12、執行tableView:viewForHeaderInSection:一次
13、執行tableView:willDisplayHeaderView:forSection:一次
14、執行tableView:viewForFooterInSection:一次
15、執行tableView:willDisplayFooterView:forSection:一次
運行結果如下:(有點丑)
當我們把section設置為2,每個section中row行數為3的時候,控制台輸出結果如下:
2016-07-27 18:17:27.739 UITableViewMethodsTest[13712:522471] ----------------------------------------------------------------------1 2016-07-27 18:17:27.739 UITableViewMethodsTest[13712:522471] numberOfSectionsInTableView: 2016-07-27 18:17:27.739 UITableViewMethodsTest[13712:522471] tableView:heightForHeaderInSection: 2016-07-27 18:17:27.739 UITableViewMethodsTest[13712:522471] tableView:heightForHeaderInSection: 2016-07-27 18:17:27.740 UITableViewMethodsTest[13712:522471] tableView:heightForFooterInSection: 2016-07-27 18:17:27.740 UITableViewMethodsTest[13712:522471] tableView:heightForFooterInSection: 2016-07-27 18:17:27.740 UITableViewMethodsTest[13712:522471] tableView:numberOfRowsInSection: 2016-07-27 18:17:27.740 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.740 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.740 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.740 UITableViewMethodsTest[13712:522471] tableView:heightForHeaderInSection: 2016-07-27 18:17:27.741 UITableViewMethodsTest[13712:522471] tableView:heightForHeaderInSection: 2016-07-27 18:17:27.741 UITableViewMethodsTest[13712:522471] tableView:heightForFooterInSection: 2016-07-27 18:17:27.741 UITableViewMethodsTest[13712:522471] tableView:heightForFooterInSection: 2016-07-27 18:17:27.741 UITableViewMethodsTest[13712:522471] tableView:numberOfRowsInSection: 2016-07-27 18:17:27.741 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.741 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.742 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.756 UITableViewMethodsTest[13712:522471] ----------------------------------------------------------------------2 2016-07-27 18:17:27.756 UITableViewMethodsTest[13712:522471] numberOfSectionsInTableView: 2016-07-27 18:17:27.757 UITableViewMethodsTest[13712:522471] tableView:heightForHeaderInSection: 2016-07-27 18:17:27.757 UITableViewMethodsTest[13712:522471] tableView:heightForHeaderInSection: 2016-07-27 18:17:27.757 UITableViewMethodsTest[13712:522471] tableView:heightForFooterInSection: 2016-07-27 18:17:27.758 UITableViewMethodsTest[13712:522471] tableView:heightForFooterInSection: 2016-07-27 18:17:27.758 UITableViewMethodsTest[13712:522471] tableView:numberOfRowsInSection: 2016-07-27 18:17:27.759 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.759 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.759 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.759 UITableViewMethodsTest[13712:522471] tableView:heightForHeaderInSection: 2016-07-27 18:17:27.760 UITableViewMethodsTest[13712:522471] tableView:heightForHeaderInSection: 2016-07-27 18:17:27.760 UITableViewMethodsTest[13712:522471] tableView:heightForFooterInSection: 2016-07-27 18:17:27.760 UITableViewMethodsTest[13712:522471] tableView:heightForFooterInSection: 2016-07-27 18:17:27.760 UITableViewMethodsTest[13712:522471] tableView:numberOfRowsInSection: 2016-07-27 18:17:27.761 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.767 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.767 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.769 UITableViewMethodsTest[13712:522471] ----------------------------------------------------------------------3 2016-07-27 18:17:27.769 UITableViewMethodsTest[13712:522471] numberOfSectionsInTableView: 2016-07-27 18:17:27.770 UITableViewMethodsTest[13712:522471] tableView:heightForHeaderInSection: 2016-07-27 18:17:27.770 UITableViewMethodsTest[13712:522471] tableView:heightForHeaderInSection: 2016-07-27 18:17:27.770 UITableViewMethodsTest[13712:522471] tableView:heightForFooterInSection: 2016-07-27 18:17:27.770 UITableViewMethodsTest[13712:522471] tableView:heightForFooterInSection: 2016-07-27 18:17:27.771 UITableViewMethodsTest[13712:522471] tableView:numberOfRowsInSection: 2016-07-27 18:17:27.771 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.771 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.771 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.771 UITableViewMethodsTest[13712:522471] tableView:heightForHeaderInSection: 2016-07-27 18:17:27.771 UITableViewMethodsTest[13712:522471] tableView:heightForHeaderInSection: 2016-07-27 18:17:27.771 UITableViewMethodsTest[13712:522471] tableView:heightForFooterInSection: 2016-07-27 18:17:27.771 UITableViewMethodsTest[13712:522471] tableView:heightForFooterInSection: 2016-07-27 18:17:27.772 UITableViewMethodsTest[13712:522471] tableView:numberOfRowsInSection: 2016-07-27 18:17:27.772 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.772 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.772 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.776 UITableViewMethodsTest[13712:522471] ----------------------------------------------------------------------4 2016-07-27 18:17:27.776 UITableViewMethodsTest[13712:522471] numberOfSectionsInTableView: 2016-07-27 18:17:27.776 UITableViewMethodsTest[13712:522471] tableView:heightForHeaderInSection: 2016-07-27 18:17:27.777 UITableViewMethodsTest[13712:522471] tableView:heightForHeaderInSection: 2016-07-27 18:17:27.777 UITableViewMethodsTest[13712:522471] tableView:heightForFooterInSection: 2016-07-27 18:17:27.777 UITableViewMethodsTest[13712:522471] tableView:heightForFooterInSection: 2016-07-27 18:17:27.777 UITableViewMethodsTest[13712:522471] tableView:numberOfRowsInSection: 2016-07-27 18:17:27.777 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.777 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.778 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.778 UITableViewMethodsTest[13712:522471] tableView:heightForHeaderInSection: 2016-07-27 18:17:27.778 UITableViewMethodsTest[13712:522471] tableView:heightForHeaderInSection: 2016-07-27 18:17:27.779 UITableViewMethodsTest[13712:522471] tableView:heightForFooterInSection: 2016-07-27 18:17:27.779 UITableViewMethodsTest[13712:522471] tableView:heightForFooterInSection: 2016-07-27 18:17:27.779 UITableViewMethodsTest[13712:522471] tableView:numberOfRowsInSection: 2016-07-27 18:17:27.779 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.780 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.781 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.784 UITableViewMethodsTest[13712:522471] tableView:cellForRowAtIndexPath: 2016-07-27 18:17:27.785 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.786 UITableViewMethodsTest[13712:522471] tableView:willDisplayCell:forRowAtIndexPath: 2016-07-27 18:17:27.787 UITableViewMethodsTest[13712:522471] tableView:cellForRowAtIndexPath: 2016-07-27 18:17:27.787 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.789 UITableViewMethodsTest[13712:522471] tableView:willDisplayCell:forRowAtIndexPath: 2016-07-27 18:17:27.790 UITableViewMethodsTest[13712:522471] tableView:cellForRowAtIndexPath: 2016-07-27 18:17:27.790 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.790 UITableViewMethodsTest[13712:522471] tableView:willDisplayCell:forRowAtIndexPath: 2016-07-27 18:17:27.791 UITableViewMethodsTest[13712:522471] tableView:cellForRowAtIndexPath: 2016-07-27 18:17:27.791 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.792 UITableViewMethodsTest[13712:522471] tableView:willDisplayCell:forRowAtIndexPath: 2016-07-27 18:17:27.823 UITableViewMethodsTest[13712:522471] tableView:cellForRowAtIndexPath: 2016-07-27 18:17:27.824 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.824 UITableViewMethodsTest[13712:522471] tableView:willDisplayCell:forRowAtIndexPath: 2016-07-27 18:17:27.824 UITableViewMethodsTest[13712:522471] tableView:cellForRowAtIndexPath: 2016-07-27 18:17:27.825 UITableViewMethodsTest[13712:522471] tableView:heightForRowAtIndexPath: 2016-07-27 18:17:27.825 UITableViewMethodsTest[13712:522471] tableView:willDisplayCell:forRowAtIndexPath: 2016-07-27 18:17:27.827 UITableViewMethodsTest[13712:522471] tableView:viewForHeaderInSection: 2016-07-27 18:17:27.828 UITableViewMethodsTest[13712:522471] tableView:willDisplayHeaderView:forSection: 2016-07-27 18:17:27.828 UITableViewMethodsTest[13712:522471] tableView:viewForFooterInSection: 2016-07-27 18:17:27.829 UITableViewMethodsTest[13712:522471] tableView:willDisplayFooterView:forSection: 2016-07-27 18:17:27.910 UITableViewMethodsTest[13712:522471] tableView:viewForHeaderInSection: 2016-07-27 18:17:27.911 UITableViewMethodsTest[13712:522471] tableView:willDisplayHeaderView:forSection: 2016-07-27 18:17:27.911 UITableViewMethodsTest[13712:522471] tableView:viewForFooterInSection: 2016-07-27 18:17:27.911 UITableViewMethodsTest[13712:522471] tableView:willDisplayFooterView:forSection:
這里也是執行了4輪。前3輪一樣。不過由於有兩個section。
第一輪~第三輪都是一樣的:
1、執行numberOfSectionsInTableView:一次
2、執行tableView:heightForHeaderInSection:兩次
3、執行tableView:heightForFooterInSection:兩次
4、執行tableView:numberOfRowsInSection:一次
5、執行tableView:heightForRowAtIndexPath:三次 (因為每個section中有3個row)
重復執行2~5
完畢。
第四輪:
前面的和前三輪是一致的。后面開始執行
續1、執行tableView:cellForRowAtIndexPath:一次
續2、執行tableView:heightForRowAtIndexPath:一次
續3、執行tableView:willDisplayCell:forRowAtIndexPath:一次
重復執行續1~續3 五次。然后繼續執行:
續4、執行tableView:viewForHeaderInSection:一次
續5、執行tableView:willDisplayHeaderView:forSection:一次
續6、執行tableView:viewForFooterInSection:一次
續7、執行tableView:willDisplayFooterView:forSection:一次
重復執行續4~續5一次(因為有兩個section,所以執行了兩次設置header和footer)
完畢
運行結果:(有點丑)
四、注意點
1、如果在設置header和footer信息的時候,View的優先級高於titile。(Views are preferred over title should you decide to provide both)
2、如果想讓header的title或者footer的title顯示不一樣的樣式(顏色,字體大小),可以用自定義view(UILabel)來實現。
3、注意復用單元格。
4、設置headerTitle樣式的時候如果這樣寫:
- (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 =@"頭部標題"; return titleView; }
其中的textColor設置無效。此時可以在這里設置:
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { UITableViewHeaderFooterView *vi = (UITableViewHeaderFooterView *)view; vi.textLabel.textColor = [UIColor redColor]; NSLog(@"%@",NSStringFromSelector(_cmd)); }
此方法是將要顯示header的時候調用的。從執行順序可以看出來tableView:viewForHeaderInsection:在tableView:willDisplayHeaderView:forSection:之前調用。但是我在tableView:willDisplayHeaderView:forSection:里面設置為什么設置無效呢?難道是因為他是自適應的原因么?有知道的可以告訴一下。