的UITableViewCell在不同尺寸的图像

的UITableViewCell在不同尺寸的图像

问题描述:

我试图用我的ReusableCell与不同尺寸的图像细胞。图像放在一个220x150黑盒子里,并且缩放比例为UIViewContentModeScaleAspectFit的UITableViewCell在不同尺寸的图像

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"NewsTableViewCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    NewsItem *item = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:item.imageUrl]]; 
    [cell.imageView setImage:[[UIImage alloc] initWithData:data]]; 
    [cell.imageView setBackgroundColor:[UIColor blackColor]]; 
    [cell.imageView setContentMode:UIViewContentModeScaleAspectFit]; 

    CGRect imageViewFrame = cell.imageView.frame; 
    imageViewFrame.size.width = 220; 
    imageViewFrame.size.height = 150 
    [cell.imageView setFrame:imageViewFrame]; 

    [cell.textLabel setText:item.title]; 

    return cell; 
} 

上面的代码导致下面的布局,并且在表格视图中滚动时图像有时会发生变化。

UITableView with images

而是非结构化的布局,我想的图像,以这样的排列:

Images in a black box

我在做什么错这个ReusableCell

EDIT1:

我试图创建的ImageView并添加此ImageView的为上海华向cell.contentView。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"NewsTableViewCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    NewsItem *item = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    UIImage *placeholderImage = [UIImage imageNamed:@"ImagePlaceholderThumb"]; //220x150 

    UIImageView *imageView = [[UIImageView alloc] initWithImage:placeholderImage]; 

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:item.imageUrl]]; 
    [imageView setImage:[[UIImage alloc] initWithData:data]]; 
    [imageView setBackgroundColor:[UIColor blackColor]]; 
    [imageView setContentMode:UIViewContentModeScaleAspectFit]; 

    CGRect imageViewFrame = imageView.frame; 
    imageViewFrame.size.width = placeholderImage.size.width; 
    imageViewFrame.size.height = placeholderImage.size.height; 
    [imageView setFrame:imageViewFrame]; 

    [cell.contentView addSubview:imageView]; 

    [cell.textLabel setText:item.title]; 

    return cell; 
} 

上面的代码导致以下:

UIImageView in superview

它像一些图像是在两个小区中可见。他们似乎不守我的imageViewFrame设置大小。你知道为什么吗?

+0

你在找什么叫做信箱和AFAIK,这是UIKit不会为你做的,除非你自己做。 – Till 2012-01-31 21:20:52

+0

那么,我该怎么做呢? – dhrm 2012-01-31 22:59:16

+0

查看评论给我自己的答案。此外,顺便说一句,您创建UIImageView并加载其数据的方式每当单元格从屏幕变为可见时就会发生。你可能会看到一些粗略的滚动。我有两个不同的方面:0)异步加载图像数据并将其缓存到EGOImageView之类的内容中,1)通常,当您创建单元格时,您希望创建单元格的子视图,您只需要在/ /配置单元格... – Keller 2012-02-01 16:54:37

速战速决将使用内容模式UIViewContentModeScaleAspectFill。图像将被拉伸到一个或两个维度以填充整个图像视图边界。

你真的需要继承UITableViewCell这样做的权利。

THRE是解决方案添加新UIImageView和使用间隔,如凯勒告诉你他的回答(随时接受他的答案,这仅仅是遗漏码)。

tableView:cellForRowAtIndexPath:提取物:

... 
cell.textLabel.text = [NSString stringWithFormat:@"Cell #%i", indexPath.row]; 
cell.imageView.image = [UIImage imageNamed:@"spacer.png"]; /* spacer is 64 x 44 */ 
/* image view width should be ~ 64 px, otherwise it will overlap the text */ 
UIImageView *iv = [[UIImageView alloc] initWithFrame:(CGRect){.size={64, tableView.rowHeight}}]; 
switch (indexPath.row) { 
    case 0: 
     iv.image = [UIImage imageNamed:@"waterfall.png"]; 
     break; 
    /* etc... */ 
} 
if (indexPath.row < 3) { 
    /* add black bg to cell w/ images */ 
    iv.backgroundColor = [UIColor blackColor]; 
} 
iv.contentMode = UIViewContentModeScaleAspectFit; 
[cell.contentView addSubview:iv]; 
... 

表看起来就像这样: aspect fit

你需要设置在现有的细胞图像视图的占位符(以上spacer.png)。它会将文本标签推向右侧。

您可以使用方面填充和消除背景色位:

iv.contentMode = UIViewContentModeScaleAspectFill; 

表看起来错误的,因为图像绘制outsite界限:

aspect fill without clipping

只是夹在边界以获得更好的结果:

iv.clipsToBounds = YES; 

aspect fill

+0

对不起,但现在对我来说这是一个解决方案。我需要这些图像来保持它们的比例。 – dhrm 2012-01-31 21:18:05

+0

比例保持不变,只有整个图像不可见。无论如何,这并不像我说的那么容易。查看更新的答案。 – djromero 2012-02-01 12:13:30

为每个单元格创建一个UIImageView子视图并将其添加到contentView。每个UIImageView都包含具有一致帧的图像,但带有选项UIViewContentModeScaleAspectFit。然后只需将UIImageView的背景颜色设置为黑色。

我刚刚证实了这一点,但您还需要创建一个占位符间隔图像,以确保textLabel不会出现问题。只需使它与图像的尺寸相同(使用字母框)。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    //spacer 
    cell.imageView.image = [UIImage imageNamed:@"placeholder"]; 

    //imageview 
    UIImageView *thumbnail = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 44)]; 
    thumbnail.tag = kThumbTag; 
    thumbnail.backgroundColor = [UIColor blackColor]; 
    thumbnail.contentMode = UIViewContentModeScaleAspectFit; 
    [cell.contentView addSubview:thumbnail]; 
    } 

    // Configure the cell... 

    cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row]; 

    cell.imageView.frame = CGRectMake(0, 0, 80, 44); 

    UIImageView *thumb = (UIImageView*)[cell.contentView viewWithTag:kThumbTag]; 
    if (indexPath.row == 0) { 
    [thumb setImage:[UIImage imageNamed:@"image1.png"]]; 
    } else { 
    [thumb setImage:[UIImage imageNamed:@"image2.png"]]; 
    } 

    return cell; 
} 

显然,这个例子是不是懒加载图像(我不知道你是从一个URL加载它们)。为此,我会使用EGOImageView或其他类似的子类。

+0

谢谢你的解决方案。你能看看我的'EDIT1'吗? – dhrm 2012-02-01 08:56:09

+0

我注意到您的占位符图像的帧大小为220 x 150 - 您的单元格的高度真的是150像素吗?如果是这样,你只需要设置cell.rowHeight(在你的笔尖或viewDidLoad中)。否则,请确保您的占位符图像是您正在查找的正确框架大小或调整其框架大小。 – Keller 2012-02-01 16:44:55

+0

实际上,你也可以使用UITableView委托方法'indentationLevelForRowAtIndexPath' – Keller 2012-02-02 22:55:02