今、私は Facebook から画像を取得しようとしていて、それをテーブルビューに入れています。
セルのデフォルトの画像ビューを使用したくありません。画像のサイズが異なる場合があるためです。
画像ビューを作成してセルに配置し、画像の高さと一致するようにセルの高さを調整するにはどうすればよいですか?
どんなヘルプでも非常に役立ちます。
ありがとう、ヴィリンド・ボラ
今、私は Facebook から画像を取得しようとしていて、それをテーブルビューに入れています。
セルのデフォルトの画像ビューを使用したくありません。画像のサイズが異なる場合があるためです。
画像ビューを作成してセルに配置し、画像の高さと一致するようにセルの高さを調整するにはどうすればよいですか?
どんなヘルプでも非常に役立ちます。
ありがとう、ヴィリンド・ボラ
UITableViewDelegate
メソッドで行の高さを指定できます- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
。そのメソッドを使用すると、組み込みのimageView
プロパティを使用できる可能性がありますUITableViewCell
imageView
編集:プロパティがあなたが望むことをさせない何らかの理由がある場合、私はのカスタムサブクラスを作ることを検討しますUITableViewCell
それは私のために次のように働きました:
ViewController.h
#import <UIKit/UIKit.h>
#import "ResizingCell.h"
@interface ViewController : UITableViewController
@property (strong, nonatomic) IBOutlet ResizingCell *Cell;
@end
ViewController.m
#import "ViewController.h"
@implementation ViewController
@synthesize Cell;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [(ResizingCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath] getHeight];
}
- (ResizingCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ResizingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
[[NSBundle mainBundle] loadNibNamed:@"ResizingCell" owner:self options:nil];
cell = [self Cell];
[self setCell:nil];
}
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", [indexPath row]]];
[cell setImage:image];
return cell;
}
@end
ResizingCell.h
#define BUFFER 20
#import <UIKit/UIKit.h>
@interface ResizingCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIImageView *myImageView;
- (void)setImage:(UIImage *)image;
- (float)getHeight;
@end
ResizingCell.m
#import "ResizingCell.h"
@implementation ResizingCell
@synthesize myImageView;
- (void)setImage:(UIImage *)image {
[[self myImageView] setImage:image];
// Because the width will be important, I'd recommend setting it here...
[[self myImageView] setFrame:CGRectMake(currFrame.origin.x, currFrame.origin.y, image.size.width, currFrame.size.height)];
}
- (float)getHeight {
return (2 * BUFFER) + [[self myImageView] image].size.height;
}
@end
コードは一目瞭然です。非常に背の高い画像でテストすると、高さが適切に変化します。