星空网 > 软件开发 > 操作系统

UITableView代理方知多少+执行顺序

一、前言

  iOS中UITableView是最常用的一个控件。看了一下UITableView的代理:UITableViewDelegate 和 UITableViewDataSource。其中UITableViewDelegate的方法有38个,UITableViewDataSource的方法有11个。下面来简单介绍一下。(方法代码有点多)

二、UITableViewDelegate方法

  UITableViewDelegate的38个方法所有的都是可选的。也就是你可以实现,也可以不实现。这里就不一一介绍了,简单的看一下这个图:

UITableView代理方知多少+执行顺序images/loading.gif' data-original="http://images2015.cnblogs.com/blog/527522/201607/527522-20160727174604622-217504719.png" />

 

三、UITableViewDataSource方法

UITableViewDataSource的11个方法中有2个是必须实现的:

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

简单看一下UITableViewDataSource代理方法:

UITableView代理方知多少+执行顺序

五、方法执行顺序问题

  这里面没有将所有的方法都考虑进去,这里只是对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] ----------------------------------------------------------------------12016-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] ----------------------------------------------------------------------22016-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] ----------------------------------------------------------------------32016-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] ----------------------------------------------------------------------42016-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:一次

运行结果如下:(有点丑)

UITableView代理方知多少+执行顺序

 

当我们把section设置为2,每个section中row行数为3的时候,控制台输出结果如下:

 

2016-07-27 18:17:27.739 UITableViewMethodsTest[13712:522471] ----------------------------------------------------------------------12016-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] ----------------------------------------------------------------------22016-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] ----------------------------------------------------------------------32016-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] ----------------------------------------------------------------------42016-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)

完毕

运行结果:(有点丑)

UITableView代理方知多少+执行顺序

 

 

四、注意点

  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:里面设置为什么设置无效呢?难道是因为他是自适应的原因么?有知道的可以告诉一下。

 




原标题:UITableView代理方知多少+执行顺序

关键词:ie

ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

2020年亚马逊品牌备案2.0详细操作流程(附图文操作步骤):https://www.ikjzd.com/articles/124512
亚马逊严击操纵评论,卖家应如何安全增评?:https://www.ikjzd.com/articles/124513
敏哥:揭秘亚马逊新品打爆核心思路之快准狠:https://www.ikjzd.com/articles/124514
未来跨境大趋势,社交电商结合Shopify独立站:https://www.ikjzd.com/articles/124515
Facebook店铺来了!今年跨境电商流量风口该怎么玩:https://www.ikjzd.com/articles/124516
亚马逊运营QA集锦(764-773):https://www.ikjzd.com/articles/124517
Lazada为优化购物体验,升级买家取消订单政策:https://www.kjdsnews.com/a/1836358.html
Lazada为优化购物体验,升级买家取消订单政策:https://www.goluckyvip.com/news/186219.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流