【发布时间】:2018-03-08 14:17:11
【问题描述】:
好的,在我的故事板中,我制作了一个包含 3 个单元格的 UICollectionView。我为其创建自定义类的单元之一显然扩展了 UICollectionViewCell:
我在我的 ViewDiDApear 中注册了该课程:
[self.collectionView registerClass:[DeviceImageCell class] forCellWithReuseIdentifier:@"Cell1"];
我还在我的自定义类中为该特定单元格添加了一个 imageView 和一个该 imageView 的出口。当我制作自定义单元格时会出现问题,它会忘记在我的故事板中设置的所有内容,也就是背景颜色,我的图像视图所在的位置等等。因此,我的 imageView 一直返回 nil。
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 0)
{
static NSString *identifier = @"Cell1";
DeviceImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImage *image = [UIImage imageNamed:@"dysart-woods-full.jpg"];
cell.imageview.image = image;
cell.backgroundColor = [UIColor blueColor];
//cell.imageview.image = image;
return cell;
}
else if(indexPath.row == 1)
{
static NSString *identifier = @"Cell2";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
return cell;
}
else
{
static NSString *identifier = @"Cell3";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
imageView.image = [UIImage imageNamed:@"dysart-woods-full.jpg"];
return cell;
}
}
【问题讨论】:
-
同样的问题,目前还没有解决办法。不幸的是,@Dmytro 的回答没有帮助。我在同一个自定义单元格中的 UILabel 上设置了文本,并且可以正常工作,但图片没有出现在 CollectionView 中。
-
你解决过这个问题吗?我在一个新项目中遇到了完全相同的问题。我之前将图像视图放在集合视图单元格中没有问题......但出于某种原因,无论我在情节提要还是在笔尖中创建单元格,ImageView = nil 但 UILabels 工作正常。 @#%)&(*
标签: ios objective-c uicollectionviewcell