ios - 如何检查 UICollectionView 有图像

标签 ios objective-c delegates uicollectionviewcell

我有 4 个 UICollectionViewCell,我想在其中强制前 2 个单元格包含图像。示例代码如下。以下是一个用例:

有 4 个 UICollectionViewCell,如果我单击每个单元格将打开相机,我们可以拍照或从图片库中选择图像。如何知道前 2 个单元格必须有图像。寻找条件来检查此逻辑。

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

    Image *image = (Image *)self.defect.imageSet[indexPath.item];
    ADRPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ADRPhotoCollectionViewCell class]) forIndexPath:indexPath];
    cell.image = image.thumbnailImage ? image.thumbnailImage : nil;
    cell.photoType = [self.defect defectPhotoTypeForIndex:indexPath];
    return cell;
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

   Image *image = self.defect.imageSet[indexPath.item];

    if (self.selectedImage != image) {

       [self updateAnnotatedImage];
        self.selectedImage = image;
       [self.jotController clearAll];
       [self updateUserInterface];

    if (image.thumbnailImage) {
        [self transitionImage:YES];
    } else {
        self.imageView.image = nil;
        [self showCameraPicker];
    }

} else if (!image.thumbnailImage) {
      //clicked the + to add a new image
      [self showCameraPicker];
  }
}

最佳答案

可以通过这种方式检查UIImageView中是否有图片:

-(BOOL) checkImages {
    for (int i=0, i<2 , i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        ADRPhotoCollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
        if (cell.imageView.image == nil || CGSizeEqualToSize(cell.imageView.image.size, CGSizeZero)) {
            //there isn't any image or the image is empty
            return NO;
        }
    }
    return YES;
}

关于ios - 如何检查 UICollectionView 有图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55084530/

相关文章:

ios - 如何使用 AVPlayer 播放 MPEG-DASH?

ios - 决定iOS外观屏幕

objective-c - 缺少桥接转换会导致预处理源错误,但不会导致真实源错误

ios - 无法将 UIViewController 限制为仅纵向

objective-c - 如何在 Objective-C 中自毁对象?

ios - 委托(delegate)方法未被称为 Objective-C

ios - Xcode 6.3.1 中没有 GameScene.swift 文件

ios - Xcode PDFKit 表单自动计算

c# - 窗口之间的交互

javascript - Phonegap 联系人。更新联系人时保存重复的电话号码