0

我有一個問題,我創建了UICollectionView與自定義單元格來顯示項目。但刷新後的UICollectionView可重複使用的單元填充錯誤索引UICollectionView Static CustomCell reuse

刷新前的UICollectionView。

enter image description here

刷新後UICollectionView。可重複使用的集合視圖細胞的

enter image description here

代碼示例。

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

    GalleryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 

    if (indexPath.item != 0) 
    { 
     [cell setCollectionItem:[collectionData_ objectAtIndex:indexPath.row - 1]]; 
    } 

    return cell; 

} 

回答

2

發生此問題的原因是單元格將被重新使用。單元被重用來改善系統的性能。如果你的表有1000個細胞時,系統不分配1000個細胞,但與添加else子句的if

if (indexPath.item != 0) 
{ 
    [cell setCollectionItem:[collectionData_ objectAtIndex:indexPath.row - 1]]; 
} 
else 
{ 
    //Set your cell at index 0 with your camera image 
    [cell setCollectionItem:@"camera-image"]; 
} 
2

一個低得多然後reuse-

嘗試認爲它是重複使用的另一個小區(氣球情況),並沒有爲第一個索引單元設置任何內容。如果您創建一個else語句來創建一個新的攝像頭單元,希望它會重新出現。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    GalleryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 

    if (indexPath.item != 0) { 
     [cell setCollectionItem:[collectionData_ objectAtIndex:indexPath.row - 1]]; 
    } else { 
     // Set camera item here 
    } 
    return cell; 
}