tableView:viewForHeaderInSection: 方法未调用

今天遇到这个问题,即重写的方法

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

可能的原因:

  1. 没有设置 delegate

  2. 没有重写方法

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

另外在第一个方法中要保证只创建一次view,否则可能会出现一些问题。

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (nowType!=3){
        return nil;
    }
    if (!self.sectionHeaderView) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 40)];
        view.backgroundColor = RGB_Color(242, 242, 242);
        UISegmentedControl *sg = [[UISegmentedControl alloc]initWithItems:@[@"item1",@"item2"]];
        sg.selectedSegmentIndex = 0;
        [sg addTarget:self action:@selector(sg3Click:) forControlEvents:UIControlEventValueChanged];
        sg.frame = CGRectMake(5, 5, Screen_Width-10, 30);
        sg.tintColor = myblue;
        [view addSubview:sg];
        self.sectionHeaderView = view;
    }
    
    return self.sectionHeaderView;
    
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,210评论 30 471
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,914评论 18 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,242评论 25 708
  • 37.cocoa内存管理规则 1)当你使用new,alloc或copy方法创建一个对象时,该对象的保留计数器值为1...
    如风家的秘密阅读 887评论 0 4
  • 有人要离婚了,总有人劝:为了孩子别离了,给孩子一个完整的家比什么都重要!婚姻,到底为谁而存在?该不该为了孩子而凑合...
    运安阁主阅读 142评论 0 0