Showing posts with label UITableView tableView viewForHeaderInSection example. Show all posts
Showing posts with label UITableView tableView viewForHeaderInSection example. Show all posts

Saturday, June 8, 2013

UITableView tableView viewForHeaderInSection example in Objective C (iOS).


UITableView tableView viewForHeaderInSection

Asks the delegate for a view object to display in the header of the specified section of the table view.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

Parameters of [UITableView tableView viewForHeaderInSection]
tableView
The table-view object asking for the view object.
section
An index number identifying a section of tableView .

Return Value
A view object to be displayed in the header of section .

Discussion of [UITableView tableView viewForHeaderInSection]
The returned object can be a UILabel or UIImageView object, as well as a custom view. This method only works correctly when tableView:heightForHeaderInSection: is also implemented.

UITableView tableView viewForHeaderInSection example.

-(CGFloat) tableView:(UITableView *)tableView
  heightForHeaderInSection:(NSInteger)section
{
    return 44.0;
}

-(UIView *) tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section
{
    UILabel *l = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
    l.backgroundColor = [UIColor clearColor];
    l.text= @"I am a Section Header";
    return l;
}

Example of [UITableView tableView viewForHeaderInSection].
- (CAGradientLayer *) greyGradient {
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.startPoint = CGPointMake(0.5, 0.0);
    gradient.endPoint = CGPointMake(0.5, 1.0);

    UIColor *color1 = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1.0];
    UIColor *color2 = [UIColor colorWithRed:240.0f/255.0f green:240.0f/255.0f blue:240.0f/255.0f alpha:1.0];

    [gradient setColors:[NSArray arrayWithObjects:(id)color1.CGColor, (id)color2.CGColor, nil]];
    return gradient;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CGFloat width = CGRectGetWidth(tableView.bounds);
    CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
    UIView *container = [[[UIView alloc] initWithFrame:CGRectMake(0,0,width,height)] autorelease];
    container.layer.borderColor = [UIColor grayColor].CGColor;
    container.layer.borderWidth = 1.0f;
    CAGradientLayer *gradient = [self greyGradient];
    gradient.frame = container.bounds;
    [container.layer addSublayer:gradient];

    UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(12,0,width,height)] autorelease];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.font= [UIFont boldSystemFontOfSize:19.0f];
    headerLabel.shadowOffset = CGSizeMake(1, 1);
    headerLabel.textColor = [UIColor whiteColor];
    headerLabel.shadowColor = [UIColor darkGrayColor];
    NSString *title = [self tableView:tableView titleForHeaderInSection:section];
    headerLabel.text = title;
    return container;
}

UITableView tableView viewForHeaderInSection example.
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSLog(@"calling view for header");
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 480, 30.0)];

if(section == 0){
// create the label object
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor blackColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:20];
headerLabel.frame = CGRectMake(20.0, 0.0, 100.0, 30.0);

headerLabel.text = @"Totals";

NSString *path = [[NSBundle mainBundle] pathForResource:@"images/minus" ofType:@"gif"];
UIImage *theImage = [UIImage imageWithContentsOfFile:path];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:theImage] autorelease];
[customView addSubview:imageView];
[customView addSubview:headerLabel];

}
else {
NSLog(@"test 2");
}
return customView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if(section == 0){
return 50;
}
return 30;

}

End of UITableView tableView viewForHeaderInSection example article.