UIImageViewがUITableViewヘッダーに表示されない
-
03-07-2019 - |
質問
問題は、バンドルから画像を取得して適切に表示できないことです。このメソッドは、tableviewを制御するView Controllerにあります。 headerView は.nibファイルのtableviewとともにロードされ、正常にロードされるいくつかのUILabels(図示せず)を含みます。アイデアはありますか?
- (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を初期化すると、既に作成したビューに画像を配置する代わりに新しいビューを作成します。
ご回答ありがとうございます。
http://developer.apple.com/をご覧ください。 library / ios /#samplecode / TableViewUpdates / Introduction / Intro.html セクションヘッダーにUIImageviewが表示されます。