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

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

问题描述

即使正在加载图片,我也会遇到图片无法显示的问题。我尝试了几种不同的方法和相同的结果......没有图像。我得到的白色矩形是Storyboard中指定的大小和颜色。我在popover中获得了正确数量的矩形。日志正确显示名称和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

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

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

将此子类设置为storyboard中单元格的自定义类,然后将其加载到 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天全站免登陆