为什么我无法将图像分配给自定义收集CollectionViewCell

时间:2016-05-09 09:59:40

标签: ios objective-c xcode

我设计了一个自定义UICollectionViewCell并添加了图片视图作为插座。然后,我将该图像视图与单元格连接起来。不知何故,我无法将图像(UIImage)分配给该图像视图。以下是我cellForItemAtIndexPath的代码。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // Service Gallary is my custom collection view cell.
    [self.collectionView registerClass:[ServiceGallaryCell class] forCellWithReuseIdentifier:collectionCellIdentifier];

    ServiceGallaryCell *serviceCollectionCell = (ServiceGallaryCell*)[collectionView  dequeueReusableCellWithReuseIdentifier:collectionCellIdentifier forIndexPath:indexPath];

    if (collectionView == self.collectionView)
    {
        serviceCollectionCell.image.image = [UIImage imageNamed:_imagesArray[indexPath.row]];
    }

    return serviceCollectionCell; 
}

2 个答案:

答案 0 :(得分:0)

试试这段代码

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {

   static NSString *CollectionViewIdentifier = @"CellIdentifier";
   ServiceGallaryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewIdentifier forIndexPath:indexPath];

  [cell.image2 setImage:[UIImage imageNamed:_imagesArray[indexPath.row]]]

  return serviceCollectionCell; 
}

答案 1 :(得分:0)

您可以尝试:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

   static NSString *CollectionViewIdentifier = @"CellIdentifier";
   ServiceGallaryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewIdentifier forIndexPath:indexPath];

   cell.imageView.image = _imagesArray[indexPath.row];

   return serviceCollectionCell; 
}