Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView not displaying in UITableView header

My problem is that I can't seem to get the image from my bundle to display properly. This method is in the view controller that controls the tableview. headerView is loaded with the tableview in the .nib file and contains a few UILabels (not shown) that load just fine. Any ideas?

- (void)viewDidLoad {
    [super viewDidLoad];

    [[self view] setTableHeaderView:headerView];
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *imagePath = [bundle pathForResource:@"awesome_lolcat" ofType:@"jpeg"];
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    imageView = [[UIImageView alloc] initWithImage:image];
}
like image 262
kubi Avatar asked Mar 01 '23 06:03

kubi


1 Answers

FIrst you need to figure out whether your image is loading properly. The quickest way to get an image is to use the UIImage convenience method +[UIImage imageNamed: rawImageName].

Also, is this a UITableViewController? (it's unclear, but implied).

Where is imageView being used? You create it near the bottom, but don't seem to do anything with it. You probably want to create the image view, assign it an image, and then add it as a subview to the headerView.


   //this assumes that headerView is an already created UIView, perhaps an IBOutlet

   UIImage     *image = [UIImage imageNamed: @"awesome_lolcat.jpeg"];
   UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
   [headerView addSubview: [imageView autorelease]];
   [[self view] setTableHeaderView: headerView];

like image 171
Ben Gottlieb Avatar answered Mar 07 '23 10:03

Ben Gottlieb