2012-03-07 35 views
0

我對不同部分的多個部分標題有疑問。不同部分的自定義分組UITable標題

我對第一節標題文本有以下代碼。但是,在第二部分中,它顯示與第一部分相同的標題文本。我可以知道如何修改代碼,以便我可以在第二個標題中顯示其他文本嗎?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
// create the parent view that will hold header Label 
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)]; 

// create the button object 
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
headerLabel.backgroundColor = [UIColor clearColor]; 
headerLabel.opaque = NO; 
headerLabel.textColor = [UIColor whiteColor]; 
headerLabel.highlightedTextColor = [UIColor whiteColor]; 
headerLabel.font = [UIFont boldSystemFontOfSize:20]; 
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0); 

// If you want to align the header text as centered 
// headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0); 

headerLabel.text = @"section 1"; // i.e. array element 
[customView addSubview:headerLabel]; 

return customView; 
} 

回答

2

您必須爲每個部分使用不同的customView。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)]; 
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
    headerLabel.backgroundColor = [UIColor clearColor]; 
    headerLabel.opaque = NO; 
    headerLabel.textColor = [UIColor whiteColor]; 
    headerLabel.highlightedTextColor = [UIColor whiteColor]; 
    headerLabel.font = [UIFont boldSystemFontOfSize:20]; 
    headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0); 

    // If you want to align the header text as centered 
    // headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0); 
if(section == 1) 
    headerLabel.text = @"section 1"; // i.e. array element 
else if(section == 2) 
    headerLabel.text = @"section 2"; // i.e. array element 
else 
    headerLabel.text = @"section 3";//and so on 
    [customView addSubview:headerLabel]; 

    return customView; 
} 
2

而是這個

headerLabel.text = @"section 1"; // i.e. array element 

使用此

headerLabel.text=[arrayContainingSectionName objectAtIndex:section];