如何为不同的部分提供不同的标题部分标题

时间:2011-08-03 10:51:11

标签: iphone objective-c

- (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个部分,这是一个部分,那么我该如何设置标题

5 个答案:

答案 0 :(得分:2)

您需要实施

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

UITableView的委托方法。

答案 1 :(得分: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;

}

答案 2 :(得分: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;

       }

答案 3 :(得分:0)

你需要知道标题,只需使用NSArray,使其成为全局alloc并在viewDidLoad中启动它,并在dealloc中释放数组。

答案 4 :(得分: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;
}

在这里你可以插入你的自定义单元格,你可以使用该部分并获得那些部分,你可以使用开关盒并轻松访问该部分。