2012-05-07 27 views
0

現在我試圖從Facebook上拉取圖像,並將其放置在表格視圖中。TableView中的UIImage的自定義子視圖

我不想使用單元格的默認圖像視圖。因爲圖像大小可能會有所不同。

如何製作圖像視圖並將其放入單元格中,然後調整單元格高度以使其與圖像高度匹配?

任何幫助什麼都會很有幫助。

感謝, Virindh博拉

回答

0

您可以在UITableViewDelegate方法- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath指定行的高度。這可能是使用該方法,你可以使用UITableViewCell

編輯的內置imageView屬性: 如果有一些原因,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 

該代碼應該是不言自明的。當我用一個非常高的圖像測試它時,它會適當地改變高度。