Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a UIButton in the header of UITableView header

I need to place a button immediately above a dynamically populated UIViewTable. It feels right to not populate the first cell (row 0), but rather utilize the header area, so I use the UITableViewDelegate method to programmatically create a UIView containing a UIButton:

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

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

    [headerView addSubview:reportButton];
    return headerView;    
}

However, as depicted below, the button is not given the necessary space (I expected the height of the header to adhere to the 44 argument).

What is wrong here? I should add that the UITableView is created in a separate XIB-file.

alt text

like image 828
maralbjo Avatar asked Oct 05 '10 17:10

maralbjo


1 Answers

You also have to implement tableView:heightForHeaderInSection: on your table view delegate.

like image 57
Kris Markel Avatar answered Oct 14 '22 04:10

Kris Markel