gpt4 book ai didi

ios7 - UICollectionView 图像未显示

转载 作者:行者123 更新时间:2023-12-02 21:40:21 33 4
gpt4 key购买 nike

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

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

我的目标是iOS7。运行 Xcode 5.0.2。图像来自 SQL 数据库。这是一个实时应用程序,我正在将其更新到完整的 iOS7,并且将自定义网格布局替换为 UICollectionView。使用嵌入到 NavigationController 中的 CollectionViewController,可通过 UIButton 进行访问。如您所见,没有自定义单元格。这些图像将显示在弹出窗口中,然后用户可以选择它来更改底层 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 Controller 中创建一个小方法来设置图像。

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

最佳答案

问题在于图像未在单元格的 View 层次结构中设置。为此,请子类化 UICollectionViewCell 并在子类中创建一个 imageView 属性:

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

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

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

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

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

//...

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

希望这有帮助。

关于ios7 - UICollectionView 图像未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20566393/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com