开发者

how to give different header section title for different sections

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInS开发者_StackOverflow社区ection:(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;
}

this is for one section if i have 9 section then how can i set title


You need to implement

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

delegate method for UITableView.


This might solve your problem:

-(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;

}


Try this!

 - (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;

       }


You need to know the titles, just use an NSArray, make it global alloc and init it in viewDidLoad and release the array in dealloc.


- (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;
}

Here u can insert your custom cell also u can use the Section and get those section wise you can use switch case and easily access the section.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜