찾다

 >  Q&A  >  본문

objective-c - tableHeaderview问题

橘黄色区域是创建的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;

}

为情所困为情所困2764일 전670

모든 응답(2)나는 대답할 것이다

  • 迷茫

    迷茫2017-04-26 09:04:25

    너. . . . 높이를 반환하는 목적은 무엇입니까? 그냥 돌아가서 헤더에 뷰 리턴을 넣으세요

    회신하다
    0
  • 習慣沉默

    習慣沉默2017-04-26 09:04:25

    버튼 생성 시:
    btn.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin; //이것을 추가하면 OK

    회신하다
    0
  • 취소회신하다