简体   繁体   中英

adding a UIButton to tableView headerView

I am trying to add a UIButton to a tableView, however when I do the following:

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frameWidth, 200)];

UIButton *addSource = [UIButton buttonWithType:UIButtonTypeCustom];
[addSource addTarget:self action:@selector(addBoard:) forControlEvents:UIControlEventTouchUpInside];
[addSource setImage:[UIImage imageNamed:@"addsource.png"] forState:UIControlStateNormal];
[addSource setBackgroundColor:[UIColor grayColor]];
[headerView addSubview:addSource];

self.tableView_.tableHeaderView = headerView;

I didn't see a UIButton there. When I try using a UILabel it is there. Why is this?

您的addSource按钮没有任何框架。

Use this code ...

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

     UIButton *headername = [[UIButton alloc]initWithFrame:CGRectMake(20, 5, 270, 34)];
     headername.backgroundColor = [UIColor greenColor];

     UIView *headerView = [[UIView alloc] init];
     UIImageView *tempimage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 300,34)];
     tempimage.image = [UIImage imageNamed:@"GrayButton.png"];
     [headerView addSubview:tempimage];
     [headerView addSubview:headername];
     return  headerView;

}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
     UIButton *name = [[UIButton alloc]initWithFrame:CGRectMake(30, 10, 120, 45)];
     name.backgroundColor = [UIColor greenColor];
     UIView *header = [[UIView alloc] init];
     UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 200,45)];
     image.image = [UIImage imageNamed:@"Button.png"];
     [header addSubview:image];
     [header addSubview:name];
     return  header;
}

Please try this one

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 45)];

replace it with

UIButton *addSource = [UIButton buttonWithType:UIButtonTypeCustom];

this will show button at 0,0 point width 100 and height 45 in header view. this will help you

设置ur按钮的框架。按ur按钮的默认框架将为CGRectZero手段。宽度n高度都将为零。.因此将其框架设置为在相对位置绘制。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{        
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)] autorelease]; 

    UIButton *addSource = [UIButton buttonWithType:UIButtonTypeRoundedRect];    
    addSource.frame = CGRectMake(80.0, 0, 160.0, 40.0);
    [addSource setTitle:@"rep" forState:UIControlStateNormal];
    [addSource addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];        

    [headerView addSubview:addSource];
    return headerView;    
}

Also dont forget to implement.

tableView:heightForHeaderInSection: 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM