如何为 CollectionViewCell 设置两个图像视图

How to set two image view for CollectionViewCell

我有一个 ImagePickerController 用于照片和视频。当我从相机中选择照片或视频时,我将其显示在 CollectionView 中作为图像。在这一步一切正常。我添加了一个 Bool 变量 checkVideo 来知道我在单元格中有什么图像,来自视频或照片的图像。 checkVideo 当我将 imagePickerController 用于视频或照片时更改值。我想为来自视频的图像添加带有播放图标的新图像。这是我的代码:

- (CustomCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

    if (checkVideo == YES) {

        UIImageView *playVideoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        playVideoImageView.image = playImg;
        [cell addSubview:playVideoImageView];
        cell.imageViewCell.image = arrayForImages[indexPath.row];
        checkVideo = NO;
    } else {
        cell.imageViewCell.image = arrayForImages[indexPath.row];
    }
    NSLog(@"check video %@", checkVideo ? @"YES" : @"NO");
    return cell;
}

您目前面临的问题是什么?

您可以在自定义单元格中添加一个图像视图,并根据 checkvideo bool 隐藏该图像。

- (CustomCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

    if (checkVideo == YES) {

      /*  UIImageView *playVideoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        playVideoImageView.image = playImg;
        [cell addSubview:playVideoImageView];*/
        cell.imageViewCell.image = arrayForImages[indexPath.row];
        checkVideo = NO;
    } else {
        cell.imageViewCell.image = arrayForImages[indexPath.row];
    }
    NSLog(@"check video %@", checkVideo ? @"YES" : @"NO");

//您可以根据下行隐藏或显示播放图像。 cell.playimage.hidden = !checkVideo;

    return cell;
}

您可能需要制作两个原型,而不是一个: 故事板的 collectionView 场景中的 MyCell1 和 MyCell2,然后:

- (CustomCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell1" forIndexPath:indexPath];

    // previously, you were using the same variable, that always had the same value for each row... 
    // you should grab a value from your prepared array like this:

    Bool checkVideo = checkVideoArray[indexPath.row]

    if (checkVideo == YES) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell2" forIndexPath:indexPath];        
        cell.imageViewCell.image = arrayForImages[indexPath.row];

        // in your prototype MyCell2 you now have to add one more subView like 'playImageView'
        // don't forget to link it via @IBOutlet too
        // reuse handling will be safe here - because if you were adding subview each time in cellForRow method, the image wouldn't be handled properly and it would be re-adding a layer on layer, what wouldn't be OK
        cell.playImageView.image = playImg;                         
    } else {
        cell.imageViewCell.image = arrayForImages[indexPath.row];
    }
    NSLog(@"check video %@", checkVideo ? @"YES" : @"NO");
    return cell;
}

在我看来,您可能使用单个 Boolean 变量 checkVideo 来区分视频和照片。这可能是它适用于照片的原因,但是当录制视频时,所有图像都会通过播放按钮投射为视频。因此,您可能需要将标志重新设计为布尔数组。 如图所示设置标志。

[checkVideo replaceObjectAtIndex:index withObject:[NSNumber numberWithBool:YES]]

或者使用 @(YES) 将 BOOL 包装在 NSNumber

[checkVideo replaceObjectAtIndex:index withObject:@(YES)]]

现在您可以通过从数组中提取 boolValue 来检查与正在考虑的单元格对应的 checkVideo 标志实例的值。

BOOL 视频 = [[checkVideo objectAtIndex:indexPath.row] boolValue];

if video == YES{
    //do your stuff here
}

PS:请在问题中陈述所有这些事实或在评论中回答澄清。这将有助于指出确切的错误。