0

我有一個ImagePickerController的照片和視頻。當我從相機中選取照片或視頻時,我將其作爲圖像顯示在CollectionView中。在這一步,一切正常。我添加了一個Bool變量checkVideo,以瞭解我在單元格中顯示的圖像,來自視頻或照片的圖像。 checkVideo更改值,當我使用imagePickerController的視頻或照片。我想添加一個新的圖像與來自視頻的圖像的播放圖標。這是我的代碼:如何爲CollectionViewCell設置兩個圖像視圖

- (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; 
} 
+0

你在哪裏設置了'checkVideo'標誌?它是單個變量還是「數組」? – Mathews

回答

1

您可能需要做出兩個原型,而不是一個: MyCell1和MyCell2在你的故事板的的CollectionView場景,然後:

- (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; 
} 
+0

我試圖使用你的解決方案,但我有相同的結果,當我把照片一切正常,但使用視頻時,播放圖標爲所有單元格添加。但我還有一個問題。如果我使用xib,我應該添加另一個自定義單元格用於播放圖像(圖標),或者我只能使用一個自定義單元格,並只註冊nib'[self.collectionView registerNib:[UINib nibWithNibName:@「CustomCell」bundle:nil] forCellWithReuseIdentifier:@ 「MyCell2」];」 –

+0

我沒有注意到,我們忘記爲您的checkVideo變量獲得正確的值。檢查更新。 – pedrouan

+0

謝謝!!!它工作完美!你拯救我的一天! –

0

您目前面臨的問題是什麼?

您可以在您的自定義單元格中添加一個圖像視圖,並基於checkvideo布爾圖像將被隱藏。

- (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; 
} 
+0

我嘗試這種方法,如果我先拍照片播放按鈕不顯示其正確,但是當我錄製視頻播放按鈕顯示在所有單元格 –

+0

如果我更改我的代碼爲'if(checkVideo == YES){ cell。 playImage.hidden =!checkVideo; cell.imageViewCell.image = arrayForImages [indexPath.row]; } else { cell.imageViewCell.image = arrayForImages [indexPath.row]; } checkVideo = NO; return cell;'現在當我錄製視頻時添加了第一個單元格時播放圖標,並且添加了照片播放圖標後,移動到了1,2,3單元格等。 –

+0

因爲您正在管理只有一個布爾顯示視頻或沒有。 如果將有多行,那麼肯定會出現問題,您需要管理arrayForVideo,它將與arrayforimage具有相同數量的bool對象。 if([arrayForVideo objectatindex item == == YES) – Bhavik

1

我看到它的方式,你可能會使用單Boolean變量checkVideo用於區分視頻和照片。這可能是爲什麼它可以很好地處理照片,但是當錄製視頻時,所有圖像都會通過「播放」按鈕作爲視頻進行播放。所以可能你可能需要將你的標誌重新設計成一個布爾數組。 如圖所示設置標誌。

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

或通過使用@(YES),該包裝一個BOOL在一個NSNumber

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

現在你可以檢查checkVideo標誌實例的通過從拉出boolValue對應於單元所考慮的值陣列。

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

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

PS:請在問題中說明所有這些事實或回答評論中的說明。這將有助於引腳指出確切的錯誤。

+0

我初始化一個可變陣列,並添加到它的布爾值,但是當你嘗試比較你寫我有一個警告**指針和整數('id'和'int')之間的比較**我的代碼'if(checkVideoArray [ indexPath.row] == YES)' –

+0

在回答中也加了 – Mathews

+0

謝謝解答!我從@pedrouan解決方案解決了我的問題。但你的解決方案也是對的!謝謝! –