我的问题是我似乎无法从我的包中获取图像以便正确显示。此方法位于控制tableview的视图控制器中。 headerView 在.nib文件中加载了tableview,并包含一些加载得很好的UILabel(未显示)。有什么想法吗?

- (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];
}
有帮助吗?

解决方案

如果您已经创建了一个插座并将其连接到Interface Builder中的视图,则应该使用该视图,而不是动态创建UIImageView。


   //this assumes that headerView is an already created UIView, perhaps an IBOutlet
   //also, imageViewOutlet is an IB outlet hooked up to a UIImageView, and added as
   //a subview of headerView.

   //you can also set the image directly from within IB
   imageViewOutlet.image = [UIImage imageNamed: @"awesome_lolcat.jpeg"];
   [[self view] setTableHeaderView: headerView];

其他提示

首先,你需要确定你的图像是否正确加载。获取图像的最快方法是使用UIImage便捷方法+ [UIImage imageNamed:rawImageName]。

另外,这是一个UITableViewController吗? (目前还不清楚,但暗示)。

使用imageView在哪里?你在底部附近创建它,但似乎没有做任何事情。您可能想要创建图像视图,为其指定图像,然后将其作为子视图添加到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];

以编程方式添加子视图,但它未正确链接到Interface Builder中的 UIImageView 。我创建的视图(我认为)正确链接到我的 UITableViewController 中的UIView插座,但是当我初始化我的imageView时,它会创建一个新视图,而不是将图像放入我已创建的视图中。 / p>

感谢您的回答。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top