在我的故事板中,我使用了一个带有3个单元格的UICollectionView。其中一个单元格,我为其创建了一个自定义类,该类显然是UICollectionViewCell的扩展:
我在ViewDiDApear中注册了这个类:
[self.collectionView registerClass:[DeviceImageCell class] forCellWithReuseIdentifier:@"Cell1"];
我在自定义的单元格中添加了一个imageView,并在自定义类中添加了它的outlet。问题出现在我制作自定义单元格时,它忘记了在storyboard中设置的所有内容,比如背景颜色和图像视图等。由于这个问题,我的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;
}
}