0

Can anyone help me figure out how to add and image to the header view? The code I tried using doesn't work. Below is what I have so far.

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{

    UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView*)view;
    if (headerView != nil)
    {

        headerView.contentView.backgroundColor = [UIColor greenColor];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(200.0f, 0.0f, 100.0f, 25.0f)];

        *imageView.image = [UIImageView = image.png];* //This line doesn't work.

        [headerView.contentView addSubview:imageView];

        UILabel *mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 25.0f)];
        mainLabel.text = [sectionTitles objectAtIndex:section];
        mainLabel.backgroundColor = [UIColor redColor];
        [headerView.contentView addSubview:mainLabel];

        UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 26.0f, 150.0f, 20.0f)];
        secondLabel.text = @"Second Label Text";

        secondLabel.backgroundColor = [UIColor yellowColor];
        [headerView.contentView addSubview:secondLabel];

    }

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579

3 Answers3

1

Please Try this

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIImage *aImage = [UIImage imageNamed:@"myHeaderImage.png"];
    UIImageView *aImgView = [[UIImageView alloc] initWithImage:aImage];
    aImgView.frame = CGRectMake(5, 5, 310, 60);
    return aImgView;
}

OR plz try this , this.

Community
  • 1
  • 1
Yasika Patel
  • 6,356
  • 3
  • 19
  • 21
1

--Using this you can directly set image to your headr view.

[header setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"test.png"]]];
0

You can try out like this...

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // create the parent view that will hold header Label
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,100)];

    // create image object

    // create the label objects
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    headerLabel.backgroundColor = [UIColor clearColor];

    headerLabel.frame = CGRectMake(22,8,300,40);
    headerLabel.textColor = [UIColor whiteColor];


    headerLabel.text=@"Header text";

    UIImageView* image = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,50)];
    image.image=[UIImage imageNamed:@"Yourimage.jpg"];
    [customView addSubview:image];
    // create the imageView with the image in it
    customView.backgroundColor=[UIColor clearColor];
    [customView addSubview:headerLabel];
    return customView;


}

It worked great for me, give it a try..

Geet
  • 2,427
  • 2
  • 21
  • 39