xonegirlz
xonegirlz

Reputation: 8977

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?

Upvotes: 0

Views: 570

Answers (6)

The iOSDev
The iOSDev

Reputation: 5267

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

Upvotes: 0

ichanduu
ichanduu

Reputation: 425

- (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: 

Upvotes: 0

Nitin
Nitin

Reputation: 7471

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;

}

Upvotes: 1

user1316896
user1316896

Reputation:

-(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;
}

Upvotes: 1

Allamaprabhu
Allamaprabhu

Reputation: 865

Set frames for ur button.by defalult frame of ur button wil be CGRectZero means.width n height both wil be zero..so set its frame to draw at relative position.

Upvotes: 0

Jonathan Cichon
Jonathan Cichon

Reputation: 4406

your addSource button does not have any frame.

Upvotes: 1

Related Questions