UICollectionView中重用的单元格显示多个UIImageViews时,它们应该只显示一个

Jim*_*ery 29 objective-c uiimage ios uicollectionview

我遇到了问题UICollectionView.最初它显示正常,显示一个单元格网格,每个单元格都有一个单元格UIImageView.这些UIImageViews显示的PNG具有透明度,存储在应用程序包中.

我的问题是,一旦UICollectionView滚动,一些单元格似乎已损坏.

损坏的单元格显示多个图像堆叠在一起,最顶层的图像是它应该显示的图像,下面的图像是应该在其他单元格中使用的图像.

我最好的猜测是,这与a UICollectionView中的细胞重用方式有关,但我愿意接受建议.

这是我用于在以下内容中创建单元格的委托代码UICollectionView:

// creates the individual cells to go in the menu view
- (UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    // create collection view cell
    UICollectionViewCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    // create a uiview where we can place all views that need to go into this cell
    UIView * contents=[[UIView alloc] initWithFrame:cell.contentView.bounds];
    [contents setBackgroundColor:[UIColor clearColor]];
    [cell.contentView addSubview:contents];

    // add a button image
    NSString * buttonPath=[[NSBundle mainBundle] pathForResource:@"button" ofType:@"png" inDirectory:[[buttons objectAtIndex:indexPath.row] objectForKey:@"name"]];
    UIImage * button=[UIImage imageWithContentsOfFile:buttonPath];
    UIImageView * buttonView=[[UIImageView alloc] initWithImage:button];
    [buttonView setContentMode:UIViewContentModeScaleAspectFit];
    [buttonView setFrame:contents.bounds];
    [contents addSubview:buttonView];

    // set tag to the indexPath.row so we can access it later
    [cell setTag:indexPath.row];

    // add interactivity
    UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)];
    [tap setNumberOfTapsRequired:1];
    [cell addGestureRecognizer:tap];

    // return the cell
    return cell;

}
Run Code Online (Sandbox Code Playgroud)

如果需要,我可以提供更多代码.

如何阻止细胞腐败?

Tot*_*mus 42

问题是你不断添加视图,UICollectionViewCell因为它们被自动重用UICollectionView.所以旧UIImageView的仍然在细胞上,因为你正在增加一个cellForItemAtIndexPath:被称为.

不要使用addSubview:!

相反,您可以创建一个自定义单元格,其中包含您想要的所有视图.因此,当cellForItemAtIndexPath:调用时,您只需要设置此CustomCollectionViewCell的内容.

这样它肯定会不会被破坏.


如何构建CustomCell.

第1步:创建.h和.m类.

CustomCell.h

#import <UIKit/UIKit.h>

@interface CustomCell : UICollectionViewCell
{
    UIImageView *imageView;
}

@property (nonatomic, retain) UIImageView *imageView; //this imageview is the only thing we need right now.

@end
Run Code Online (Sandbox Code Playgroud)

CustomCell.m

#import "CustomCell.h"

@implementation CustomCell

@synthesize imageView;

- (id)initWithFrame:(CGRect)aRect
{
    if (self = [super initWithFrame:aRect])
    {
         //we create the UIImageView in this overwritten init so that we always have it at hand.
         imageView = [UIImageView alloc] init];
         //set specs and special wants for the imageView here.
         [self addSubview:imageView]; //the only place we want to do this addSubview: is here!

         //You wanted the imageView to react to touches and gestures. We can do that here too.
         UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)];
        [tap setNumberOfTapsRequired:1];
        [self addGestureRecognizer:tap];


        //We can also prepare views with additional contents here!
        //just add more labels/views/whatever you want.
    }
    return self;
}

-(void)onButtonTapped:(id)sender
{
    //the response to the gesture.
    //mind that this is done in the cell. If you don't want things to happen from this cell.
    //then you can still activate this the way you did in your question.

}
Run Code Online (Sandbox Code Playgroud)

第二步:导入它!现在我们创建了CustomCell,我们可以在我们想要使用它的类中导入它.

第3步:使用它!

// creates the individual cells to go in the menu view
- (CustomCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    // create collection view cell
    CustomCell *cell = (CustomCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath]; //this is the place where the CustomCell does his magic.
    //Make sure to use the CustomCellReuseId that you register in the viewdidload/loadview (step4)

    // add a button image
    NSString * buttonPath=[[NSBundle mainBundle] pathForResource:@"button" ofType:@"png" inDirectory:[[buttons objectAtIndex:indexPath.row] objectForKey:@"name"]];

    cell.imageView.image = [UIImage imageWithContentsOfFile:buttonPath]; //place the image on the CustemCell.imageView as we prepared.

    // set tag to the indexPath.row so we can access it later
    [cell setTag:indexPath.row]; //we don't need this to access the cell but I left this in for your personal want.

/*
 * we can now do this from the CustomCell as well!
 *
    // add interactivity
    UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)];
    [tap setNumberOfTapsRequired:1];
    [cell addGestureRecognizer:tap];
*/
    // return the cell
    return cell;

}
Run Code Online (Sandbox Code Playgroud)

步骤4:将单元格注册到collectionView

在viewDidLoad/loadView中添加以下行:

[_collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:@"CustomCell"];
Run Code Online (Sandbox Code Playgroud)

第五步:享受!您的CustomCell已完成.现在做任何你喜欢的事情,也不要忘记喝点咖啡.


its*_*dra 6

之后

UICollectionViewCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

只需添加此行,

[[[cell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
Run Code Online (Sandbox Code Playgroud)