1

當我在UICollectionView中來回滾動時,我注意到內容在屏幕上出現和消失的單元格發生了變化。阻止UICollectionView中每個UICollectionViewCell中的內容隨着滾動而改變?

有什麼辦法可以防止這種情況發生? 我只是想適當地加載所有的內容到每個單元格,然後阻止它隨着滾動而改變。

下面是一些與此相關的代碼:

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

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor clearColor]; 

    UILabel *label = (UILabel *)[cell viewWithTag:100]; 
    if (!label) { 
     label = [[UILabel alloc] initWithFrame:cell.bounds]; 
     label.tag = 100; 
     label.text = [self.array objectAtIndex:indexPath.row]; 
     label.textAlignment = NSTextAlignmentCenter; 
     label.textColor = [UIColor blackColor]; 
     [cell.contentView addSubview:label]; 
    } 

    return cell; 
} 

Before After

回答

2

的問題是,你是不是在你的cellForRowAtIndexPath:提供的標籤內容。請記住,這些細胞正在重用。所以即使標籤已經存在,您也需要提供標籤文本,因爲單元格現在被用於不同的行,因此需要不同的文本!

所以只是移動這一行:

label.text = [self.array objectAtIndex:indexPath.row]; 

...下來,if外塊,來回報您的cell之前。

+0

感謝您的解釋:)! – justinSYDE

相關問題