Say you want to change the look of the header in a UITableView with sections.

Here’s one way you could do it:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
	// create the parent view that will hold header Label
	UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 22.0)];
	
    UIImage *image = [UIImage imageNamed:@"Background_320x22"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    
	// 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:10];
	headerLabel.frame = CGRectMake(0.0, 0.0, 320.0, 22.0);
    
	headerLabel.text = @"My Header"; // i.e. array element
    
    [customView addSubview:imageView];
	[customView addSubview:headerLabel];
    
	return customView;
}

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

Basically you create your own UIView and then add to it whatever you want (in this case a background image and a label over top of that).

Advertisement