[英]UICollectionView images
我有一個帶有圖像的UICollectionView,現在我想為從第5個圖像到最后的所有圖像添加一個標簽。 我使用下面的代碼,並且可以正常工作,但是當我向下滾動到末尾然后向后滾動時,所有標簽都會顯示在“所有圖像”上。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.layer.borderWidth = 0.3;
recipeImageView.layer.borderColor = [UIColor blackColor].CGColor;
recipeImageView.image = [UIImage imageNamed:[wallPack objectAtIndex:indexPath.row]];
// add Paid stickers
NSString *stickName = [NSString stringWithFormat:@"stickerImage.png"]; /// image name
UIImage *stickImage = [UIImage imageNamed:stickName];
stickView = [[UIImageView alloc] initWithImage:stickImage];
stickView.contentMode = UIViewContentModeScaleAspectFit;
if (indexPath.row >= 5) {
[recipeImageView addSubview:stickView];
}
return cell;
}
問題出在哪兒? 為什么在向下滾動之前,所有內容從5號到最后都按我的意願顯示,但是當我向后滾動時,不干膠標簽圖像將粘貼在所有圖像上。
單元被重用。 您必須將標簽添加到您想要將其添加到,如果它不存在了的,而且從那些地方你不想要它,如果它的存在將其刪除。
我建議創建UICollectionViewCell
子類,您可以在其中添加stickImage屬性並進行設置(根據您的條件)。 此外,在prepareForReuse
函數中,您可以將其默認設置為所需的值,這也會使- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
更加整潔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.