2011-08-03 35 views
0
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section]; 
if (sectionTitle == nil) { 
    return nil; 
} 

UILabel *label = [[[UILabel alloc] init] autorelease]; 
label.frame = CGRectMake(20, 6, 300, 30); 
label.backgroundColor = [UIColor clearColor]; 
label.textColor = [UIColor colorWithHue:(136.0/360.0) // Slightly bluish green 
          saturation:1.0 
          brightness:0.60 
            alpha:1.0]; 
label.shadowColor = [UIColor whiteColor]; 
label.shadowOffset = CGSizeMake(0.0, 1.0); 
label.font = [UIFont boldSystemFontOfSize:16]; 
label.text = sectionTitle; 

// Create header view and add label as a subview 
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)]; 
[view autorelease]; 
[view addSubview:label]; 

return view; 
} 

這是一個部分,如果我有9節我再怎麼可以設置標題如何給不同的頁眉部分的標題爲不同的部分

+0

您看到了什麼問題? –

+1

'section'參數告訴你tableview當前正在處理哪一部分。如果這不是你問的,你應該讓你的問題更清楚。 –

回答

1

試試這個!

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
    { 
    UIView *v =[[[UIView alloc] init] autorelease]; 

    v.backgroundColor = [UIColor clearColor]; 


    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 7, tableView.bounds.size.width - 10, 18)] autorelease]; 

    label.textColor = [UIColor whiteColor]; 

    label.font = [UIFont boldSystemFontOfSize:15]; 

    label.backgroundColor = [UIColor clearColor]; 

    if (section == 1) { 

     label.text = @"Section 1"; 

     [v addSubview:label]; 
    } 
    else if (section == 2) { 

     label.text = @"Section 2"; 

     [v addSubview:label]; 

    } 
    else if (section == 3) { 

     label.text = @"Section 3"; 

     [v addSubview:label]; 

    } 

    return v; 

     } 
+1

您可能想了解'switch'語句。 –

2

您需要實現的UITableView

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; 

委託方法。

+1

這是最好和最簡單的答案 –

0

您需要知道標題,只需使用NSArray,使其成爲全局分配並在viewDidLoad中初始化它並釋放dealloc中的數組。

2

這可能會解決你的問題....

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIControl *containerView = [[[UIControl alloc] initWithFrame:CGRectMake(0, 0, 320, 20)] autorelease]; 

    UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 20)] autorelease]; 
    headerLabel.textColor = [UIColor colorWithRed:50.0/255.0 green:44.0/255.0 blue:37.0/255.0 alpha:1.0]; 
    [headerLabel setFont:[UIFont boldSystemFontOfSize:11.0]]; 
    //[headerLabel setFont:[UIFont boldSystemFontOfSize:13.0]]; 

    headerLabel.backgroundColor = [UIColor clearColor]; 

    if(section == 0) 
      headerLabel.text = [NSString stringWithFormat:@"First Section"]; 
    if(section == 1) 
      headerLabel.text = [NSString stringWithFormat:@"Second Section"]; 
    if(section == 2) 
      headerLabel.text = [NSString stringWithFormat:@"Third Section"]; 

    headerLabel.textColor = [UIColor colorWithRed:78.0/255.0 green:70.0/255.0 blue:59.0/255.0 alpha:1.0]; 
    //headerLabel.font = [UIFont boldSystemFontOfSize:13.0]; 
    [containerView addSubview:headerLabel]; 
    return containerView; 

} 

感謝名單.....

0
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 
    UIView *aView=[[[NSBundle mainBundle] loadNibNamed:@"tblHeader" owner:self options:nil]objectAtIndex:0]; 
    [(UILabel*)[aView viewWithTag:1] setText:[[self.reports objectAtIndex:section] valueForKey:@"domain"]]; 
    return aView; 
    return nil; 
} 

這裏u能插入你的自定義單元格也ü可以使用的部分,並得到那些部分明智的是,您可以使用開關盒並輕鬆訪問該部分。

相關問題