UICollectionView 图像未显示 [英] UICollectionView images not showing

查看:26
本文介绍了UICollectionView 图像未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an issue with the images not showing, even though they are loading. I have tried this several different ways and the same result... no images. I do get white rectangles that are the size and color specified in the Storyboard. I get the correct number of rectangles in the popover. The log shows the names and ids correctly.

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

If I call the array directly, I get the same result... white rectangles.

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

My target is iOS7. Running Xcode 5.0.2. Images are coming from a SQL database. This is a live app, which I am updating to full iOS7 and where I am swapping out a custom grid layout for UICollectionView. Using a CollectionViewController, embedded into a NavigationController, which is accessed via a UIButton. As you can see, there is no custom cell. The images are to show in a popover and are then selectable by the user to change the background of the underlying view.

- (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 控制器中创建一个小方法来设置图像.

The fix which includes actually naming the BackgroundCell (duh) in the cellForItemAtIndexPath method and creating a small method in the BackgroundCell controller to set the image.

- (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;
}

推荐答案

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

The problem is that the image is not being set in the cell's view hierarchy. To do that, subclass UICollectionViewCell and create a imageView property in the subclass:

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

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

...and init the UIImageView in its initWithFrame:

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

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

Set this subclass as the Custom Class for the cell in storyboard and then load it in the collectionView:cellForItemAtIndexPath method above setting the image to the cell's image subview:

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

//...

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

希望这会有所帮助.

这篇关于UICollectionView 图像未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆