2012-05-07 6 views
0

今、私はFacebookから画像を取り込み、それをテーブルビューに入れようとしています。TableViewのUIImageのカスタムサブビュー

私はセルのデフォルト画像ビューを使用しません。画像のサイズが変わる可能性があるからです。

イメージビューを作成してセルに配置し、イメージの高さと一致するようにセルの高さを調整するにはどうすればよいですか?

何かこれまでどんなに大きな助けになるかを助けてください。

おかげで、 Virindh Borra

答えて

0

あなたはUITableViewDelegate方法- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPathで行の高さを指定することができます。 imageViewプロパティは、あなたが欲しいものをやらせないであろうことを、いくつかの理由がある場合は私が作っ検討する、 :その方法を使用して、あなたがUITableViewCell

編集のビルトインimageViewプロパティを使用することが可能ですそれは次のように私のために働いたUITableViewCell

+0

ありがとうございますが、私はデフォルトの画像ビューを使用しようとしていません。 – user1320885

+0

なぜですか?固定サイズ以上のものがありますか?私は別の可能な解決策で私の答えを編集しました。 –

+0

これは私が探しているものです。既定のイメージビューを既に使用しています。どうすればuiimageviewのサブクラスを作成し、それをセルに追加できますか? – user1320885

0

のカスタムサブクラス:

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 

コードはわかりやすいものでなければなりません。本当に背の高いイメージでテストすると、高さが適切に変化します。