[英]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.