簡體   English   中英

TableView中UIImage的自定義子視圖

[英]Custom Subview of UIImage in TableView

現在,我正在嘗試從Facebook提取圖像,並將其放在表格視圖中。

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

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

任何幫助都將大有幫助。

謝謝,Virindh Borra

您可以在UITableViewDelegate方法中指定行的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 通過使用該方法,可以使用UITableViewCell的內置imageView屬性。

編輯:如果由於某種原因, 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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM