Adding a UIButton to the header of a UITableView

I need to place a button directly above a dynamically populated UIViewTable. It seems correct not to fill the first cell (row 0), but to use 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 shown below, the button is not set to the required space (I expected the header height to match argument 44).

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

alt text

+7
iphone uitableview
source share
2 answers

You also need to implement tableView:heightForHeaderInSection: in your table view delegate.

+14
source share

Have you implemented - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section ?

+7
source share

All Articles