set Background Image to UITableView Section Header
cocoa-touch, ios, objective-c, uitableview
Solution
If you want to set the image as section header use:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImage *myImage = [UIImage imageNamed:@"loginHeader.png"];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:myImage] autorelease];
imageView.frame = CGRectMake(10,10,300,100);
return imageView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 100;
}
Problem
I've searched online and on stackoverflow but couldn't find one good answer how to do it. I've read about ``` - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section` ``` but there's nothing there that can help me. The closest thing to an image background for the header there is: ``` headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myBackground.png"]]; ``` but it shows the picture as a pattern with no way controlling its size.