【问题标题】:UICollectionView images not showingUICollectionView 图像未显示
【发布时间】:2013-12-13 12:20:03
【问题描述】:

我遇到了图像未显示的问题,即使它们正在加载。我已经尝试了几种不同的方法和相同的结果......没有图像。我确实得到了故事板中指定的大小和颜色的白色矩形。我在弹出窗口中得到了正确数量的矩形。日志正确显示名称和 ID。

如果我直接调用数组,我会得到相同的结果...白色矩形。

我的目标是iOS7。运行 Xcode 5.0.2。图像来自 SQL 数据库。这是一个实时应用程序,我正在将其更新到完整的 iOS7,并且我正在为 UICollectionView 换出自定义网格布局。使用 CollectionViewController,嵌入到 NavigationController 中,通过 UIButton 访问。如您所见,没有自定义单元格。这些图像将显示在弹出窗口中,然后用户可以选择以更改底层视图的背景。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BckgndCell" forIndexPath:indexPath];

    NSDictionary * tempDict = [_arrayOfBckgnds objectAtIndex:indexPath.row];
    NSNumber *buttonTagNumber = (NSNumber *)[tempDict objectForKey:@"id"];
    int buttonTag = [buttonTagNumber intValue];
    NSString *tempImage = [tempDict objectForKey:@"fileName"];

    NSLog(@"Filename: %@", tempImage);
    NSLog(@"ButtonTagNumber: %@", buttonTagNumber);
    UIImageView *image = [[UIImageView alloc]init];
    image.image = [UIImage imageNamed:tempImage];
    NSLog(@"Image.image: %@", image.image);

    // needed for selecting the background in didSelectItemAtIndexPath
    _bgtag = buttonTag;

    return cell;
}

修复包括在 cellForItemAtIndexPath 方法中实际命名 BackgroundCell (duh) 并在 BackgroundCell 控制器中创建一个小方法来设置图像。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BackgroundCell *cell = (BackgroundCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"BckgndCell" forIndexPath:indexPath];

    NSDictionary * tempDict = [_arrayOfBckgnds objectAtIndex:indexPath.row];
    NSNumber *buttonTagNumber = (NSNumber *)[tempDict objectForKey:@"id"];
    int buttonTag = [buttonTagNumber intValue];
    NSString *tempImage = [tempDict objectForKey:@"fileName"];

    [cell setImage:[UIImage imageNamed:tempImage]];

    // needed for selecting the background in didSelectItemAtIndexPath
    _bgtag = buttonTag;

    return cell;
}

主单元格代码;

- (id)initWithFrame:(CGRect)frame
{
    _backgroundImage = [[UIImageView alloc] initWithFrame:self.contentView.bounds];

    [self.contentView addSubview:_backgroundImage];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)setImage:(UIImage *)image
{
    _backgroundImage.image = image;
}

【问题讨论】:

  • 我现在有图片显示...为其他工作休息了很长时间。主要的事情,调用单元控制器和其他一些事情。我在上面添加了修复。

标签: ios7 uicollectionview popover


【解决方案1】:

问题是图像没有设置在单元格的视图层次结构中。为此,将 UICollectionViewCell 子类化并在子类中创建 imageView 属性:

@interface CollectionViewCellSubclass : UICollectionViewCell
@property (nonatomic, strong) UIImageView *imageView;
@end

...并在其initWithFrame 中初始化 UIImageView:

_imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:_imageView];

将此子类设置为情节提要中单元格的自定义类,然后在上面的collectionView:cellForItemAtIndexPath方法中加载它,将图像设置为单元格的图像子视图:

CollectionViewCellSubclass *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BckgndCell" forIndexPath:indexPath];

//...

cell.imageView.image = [UIImage imageNamed:tempImage];

希望这会有所帮助。

【讨论】:

  • 这很接近,但没有雪茄。请参阅主要问题区域中的修复。
  • @MarcWatson 关键点是您已将UICollectionViewCell 子类化并将imageView 添加到其contentView。它应该已经在initWithFrame 中初始化,所以我在上面进行了编辑。答案的要点是准确的,并且可能会让您走上正确的道路,因此也许值得接受。
【解决方案2】:

dequeueReusableCellWithReuseIdentifier 行之后添加cell.contentView.frame = [cell bounds] 对我有用。

假设您通过 xib 添加自定义 (200,200) UICollectionViewCell 并在代码中的某处将其框架更改为例如(120,120)。您必须相应地设置其内容视图的框架,否则其imageView(在其内容视图上)上的图像将无法正确显示。

【讨论】:

  • 您应该将该信息发布为对您答案的编辑。我在您的回答中为您编辑了您的评论。
猜你喜欢
  • 2021-01-21
  • 1970-01-01
  • 2017-01-24
  • 1970-01-01
  • 2019-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多