objective-c - tableHeaderview问题

 恋若寒1999 发布于 2022-10-26 09:12

橘黄色区域是创建的tableHeaderView,那2行按钮本来是添加在这个tableHeaderView上的,却显示在了cell上,是什么问题???

下面是代码:

-(CGFloat)initHeadView:(NSMutableArray *)hotArray
{

CGFloat headHeight = 0.0;
//热門搜索
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Width(_searchTableview), 0)];
view.backgroundColor = [UIColor orangeColor];

if ([hotArray count] > 0)
{
    UILabel *lab = [[UILabel alloc]initWithFrame:(CGRect){Margin, 0, 100.0, Size.height}];
    lab.font = [UIFont systemFontOfSize:14.0];
    lab.textAlignment = NSTextAlignmentLeft;
    lab.textColor = [UIColor lightGrayColor];
    lab.backgroundColor = [UIColor clearColor];
    lab.text = @"熱門搜索";
    [view addSubview:lab];
    headHeight += Height(lab) + ORIGIN(view).y;

    //  热门搜索按钮
    CGFloat   w = Margin;
    NSInteger row = 1;
    CGFloat y = 0.0;
    CGFloat x = 0.0;
    
    for (int i=0; i<[hotArray count]; i++)
    {
        UIButton *btn = [self createHotButton:hotArray[i]];
        x = w;
        if(w+Width(btn)+Margin/2 > MaxWidth){
            w = Margin;
            x = w;
            row ++;
            if (row > 2) break;//  最多显示2行
        }else{
            w += Width(btn)+Margin/2;
        }
        y = ((row>1) ? (BtnHeight + Margin) : (Margin/2)) + ORIGIN(lab).y + Height(lab);
        btn.frame = CGRectMake(x, y, Width(btn), BtnHeight);
        [view addSubview:btn];
        
    }//for循环结束
    CGFloat btnsH = 0.0;
    if (row == 1)
        btnsH = Margin/2 + BtnHeight;
    else
        btnsH = BtnHeight*2 + Margin;
        
    headHeight += btnsH;
}

view.frame = CGRectMake(ORIGIN(view).x, ORIGIN(view).y, Width(_searchTableview), headHeight);
_searchTableview.tableHeaderView = view;
return headHeight;

}

2 个回答
  • 创建按钮时:
    btn.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;//添加这个就OK

    2022-10-27 00:31 回答
  • 你这。。。。 返回一个高度是要做什么?view return回去放在header 上就行了

    2022-10-27 00:31 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有