Reused cells in a UICollectionView show multiple UIImageViews when they should only show one

后端 未结 7 1818
天命终不由人
天命终不由人 2020-12-08 09:53

Im having a problem with my UICollectionView. Initially it displays fine, showing a grid of cells, each cell with a single UIImageView. These

相关标签:
7条回答
  • That Happen because you add every time a UIImageView to your cell for fix this problem you have to Make a custom cell and then use it like take:

    Custom.h

    #import <UIKit/UIKit.h>
    
    @interface CustomCell : UICollectionViewCell
    
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    
    @end
    

    Custom.m

    #import "CustomCell.h"
    
    @implementation CustomCell
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
        }
        return self;
    }
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
    }
    */
    
    @end
    

    Your Controller

    - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
        CustomCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath];
    
        NSString * buttonPath=[[NSBundle mainBundle] pathForResource:@"button" ofType:@"png" inDirectory:[[buttons objectAtIndex:indexPath.row] objectForKey:@"name"]];
        UIImage * button=[UIImage imageWithContentsOfFile:buttonPath];
    
        [cell.imageView setImage:button];
    
        return cell;
    }
    

    You have also to set 'CustomCell' like Identifier to the Cell in IB

    0 讨论(0)
提交回复
热议问题