gpt4 book ai didi

ios - 当 UICollectionView 滚动时,UICollectionViewCell 中的 subview 显示错误

转载 作者:行者123 更新时间:2023-12-03 18:17:27 25 4
gpt4 key购买 nike

我制作了一个自定义的 UICollectionViewCell 并向其 contentView 添加了一个 subview :

BooksCell.h

@interface BooksCell : UICollectionViewCell
@property (strong, nonatomic) UIImageView *certifyImageView;
@end

BooksCell.m

- (id)initWithFrame:(CGRect)frame {
self= [super initWithFrame:frame];
if(self){
_coverImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 88, 117)];
_coverImageView.userInteractionEnabled = YES;
[self.contentView addSubview:_coverImageView];

UIImage *certifyImage = [UIImage imageNamed:@"13-6.png"];
_certifyImageView = [[UIImageView alloc] initWithFrame:CGRectMake(17.5, _coverImageView.frame.size.height-3, certifyImage.size.width, certifyImage.size.height)];
_certifyImageView.image = certifyImage;
_certifyImageView.hidden = YES;
}
return self;
}

View Controller

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
BooksCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BooksCell" forIndexPath:indexPath];
cell.coverImageView.image = [UIImage imageNamed:@"13-5.png"];

// **Here I want some cell display certifyImageView and some not.**
if(indexPath.row%2==0){
cell.certifyImageView.hidden = NO;
}

return cell;
}

我将 collectionView 作为 subview 添加到 Viewcontroller,并正确设置它的框架,现在 collectionView 显示了 coverImageViewcertifyImageView 正常,但是当我滚动collectionView时,certifyImageView显示在错误的单元格上,我想这可能是Reuse Cell造成的,如何解决?

最佳答案

我认为因为它正在重用已经将 certifyImageView.hidden 设置为 NO 的单元格所以你必须将它设置回 YES也许试试这个

if(indexPath.row%2==0){
cell.certifyImageView.hidden = NO;
}
else{
cell.certifyImageView.hidden = YES;
}

通过这种方式,您可以确保将 certifyImageView 设置为隐藏(如果这是您想要的)。

关于ios - 当 UICollectionView 滚动时,UICollectionViewCell 中的 subview 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17480037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com