2010-10-05 43 views
7

我需要在動態填充的UIViewTable上方放置一個按鈕。感覺對不填充所述第一小區(行0),而是利用首標區,所以我使用的UITableViewDelegate方法以編程方式創建包含一個UIButton一個UIView:在UITableView標題的標題中添加一個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;  
} 

然而,如以下,所描繪的按鈕沒有被賦予必要的空間(我期望標題的高度符合44的參數)​​。

這裏有什麼問題?我應該補充說UITableView是在一個單獨的XIB文件中創建的。

alt text

回答

7

您是否實施了- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

+2

太糟糕了,我只能獎勵一個答案。謝謝。 – maralbjo 2010-10-05 19:01:31