Ios7 UICollectionView图像未显示
我有一个问题的图像不显示,即使他们正在加载。我试过几种不同的方法,结果都是一样的。。。没有图像。我得到的白色矩形是故事板中指定的大小和颜色。我得到了popover中正确数量的矩形。日志正确显示了名称和ID 如果我直接调用数组,我会得到相同的结果。。。白色矩形 我的目标是iOS7。运行Xcode 5.0.2。图像来自SQL数据库。这是一个实时应用程序,我正在将其更新为完整的iOS7,并将自定义网格布局替换为UICollectionView。使用嵌入导航控制器中的CollectionViewController,可通过UIButton访问导航控制器。如您所见,没有自定义单元格。图像将显示在popover中,然后由用户选择以更改基础视图的背景Ios7 UICollectionView图像未显示,ios7,uicollectionview,popover,Ios7,Uicollectionview,Popover,我有一个问题的图像不显示,即使他们正在加载。我试过几种不同的方法,结果都是一样的。。。没有图像。我得到的白色矩形是故事板中指定的大小和颜色。我得到了popover中正确数量的矩形。日志正确显示了名称和ID 如果我直接调用数组,我会得到相同的结果。。。白色矩形 我的目标是iOS7。运行Xcode 5.0.2。图像来自SQL数据库。这是一个实时应用程序,我正在将其更新为完整的iOS7,并将自定义网格布局替换为UICollectionView。使用嵌入导航控制器中的CollectionViewCont
- (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;
}
问题是图像未在单元格的视图层次中设置。为此,子类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];
希望这能有所帮助。在
行之后添加cell.contentView.frame=[cell bounds]
对我有用
假设您通过xib添加了一个自定义(200200)UICollectionViewCell
,并将其在代码中的某个位置的框架更改为例如(120120)。您必须相应地设置其内容视图的框架,否则其图像视图
(在其内容视图上)上的图像将无法正确显示。我现在有图像显示。。。为了其他工作休息了很长时间。主要的事情,调用cell控制器和其他一些事情。我的修正加在上面。这很接近,但没有雪茄。请参阅主要问题区域中的修复方法。@MarcWatson关键是您已经对UICollectionViewCell
进行了子类化,并将imageView
添加到了它的contentView中。它应该在initWithFrame
中初始化,因此我在上面进行了编辑。不过,答案的要点是准确的,可能会让你走上正确的道路,因此可能值得接受。你应该将这些信息发布为答案的编辑。我在你的回答中为你编辑了你的评论。