2012-07-05 58 views
3

我正在使用以下代碼在UITableView中添加UISegmentedControl。除了UISegmentedControl根本沒有響應用戶交互之外,一切正常。什麼可能是這個問題?在UITableView的標題中添加UISegmentedControl

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

     NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", nil]; 
     UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:itemArray]; 
     [control setFrame:CGRectMake(60.0, 0, 200.0, 40.0)]; 
     [control setSegmentedControlStyle:UISegmentedControlStylePlain]; 
     [control setSelectedSegmentIndex:0]; 
     [control setEnabled:YES]; 

     [headerView addSubview:control]; 
     return headerView; 

    } 
} 

回答

6

你也應該有相應的heightForHeaderInSection方法,看起來像這樣:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    if (section == 2) return 44.f; 
    return 0.f; 
} 

如果沒有,則控制仍可能會出現,但不會被任何觸摸區域內繪製。

+0

就是這樣,謝謝! – Anton 2012-07-05 17:30:31