Adding UIButton to UITableView section header

前端 未结 7 1147
一个人的身影
一个人的身影 2021-02-01 20:20

I have a UITableView with 1 section and for the section header, I would like to keep everything about the header the same but simply add a button on the right side.

7条回答
  •  逝去的感伤
    2021-02-01 20:48

    You can do that by using the below code, you can put any type of view in header view but you have to specify the frame for it.

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIView *view=[[UIView alloc]init];
        UIButton *addButton=[UIButton buttonWithType:UIButtonTypeContactAdd];
        addButton.frame=CGRectMake(250, 0, 100, 50);
        [view addSubview:addButton];
        [tblView.tableHeaderView insertSubview:view atIndex:0];
        //I feel like this is a bad idea
    
        return view;
    }
    

提交回复
热议问题