改变UITableView的footerView、headerView背景色正确姿势

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
    view.backgroundColor = [UIColor redColor];
}

想改变UITableView的footerView的背景色,上面是最容易的方法,但并没有用。

于是用了这个方法

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *headerView = [UIView new];
    headerView.backgroundColor = [UIColor redColor];
    return headerView;
}

能有效。

因为我项目中

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 100;
}

有很多个section,就在以上方法中做了优化,用了重用机制

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    static NSString *ID = @"headerViewIdentifier";
    UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
    if (footerView == nil) {
        footerView = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:ID];
    }
    [footerView.contentView setBackgroundColor:[UIColor blueColor]];
    return footerView;
}

还有一点有必要说明,如果要要控制某个section不展示headerView,下面方法是没有用的

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    static NSString *ID = @"headerViewIdentifier";
    UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
    if (footerView == nil) {
        footerView = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:ID];
    }
    [footerView.contentView setBackgroundColor:[UIColor blueColor]];
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return  10;
}

正确做法:

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }
    static NSString *ID = @"headerViewIdentifier";
    UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
    if (footerView == nil) {
        footerView = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:ID];
    }
    [footerView.contentView setBackgroundColor:[UIColor blueColor]];
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return  10;
}

说明- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section方法只能影响footerView的高度,不能控制foooterView是否显示(就是return 0 footerView会显示默认高度18)

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (self.type == FDOrderSearchViewControllerTypeSearchResult) {
        static NSString *ID = @"headerViewIdentifier";
        UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
        if (footerView == nil) {
            footerView = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:ID];
        }else{
            NSLog(@"===%f",footerView.frame.size.height);
        }
        [footerView.contentView setBackgroundColor:[UIColor blueColor]];
        return footerView;
    }
    return nil;
}
打印结果.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,085评论 3 38
  • 版权声明:未经本人允许,禁止转载. 1. TableView初始化 1.UITableView有两种风格:UITa...
    萧雪痕阅读 2,911评论 2 10
  • #pragma mark someValueAboutTableView 1.tableView的样式:UITab...
    潇岩阅读 943评论 0 0
  • UITableView 是 iOS 开发中必不可少的一个控件,基本上每一个项目都会有多个地方会用到。详细的了解UI...
    破夕_____________阅读 2,095评论 0 4
  • UITableView有两种样式: UITableView的结构:UITableView由头部,尾部,和中间一连串...
    degulade阅读 844评论 1 3