7

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

maralbjo
  • 963
  • 1
  • 10
  • 16

2 Answers2

14

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

Kris Markel
  • 12,142
  • 3
  • 43
  • 40
7

did you implement - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section?

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247