[ACCEPTED]-UICollectionView adding image to a cell-uicollectionviewcell

Accepted answer
Score: 16

First of all, the way you add images in 21 your cell is pretty dangerous. The reason 20 is that your cells are being reused (for 19 exemple when you scroll, or when you reloadData), and 18 these images are never removed on reuse. So 17 you will start seing them everywhere, and 16 you can even get to the point where your 15 cell contains multiple occurrences of the 14 image. Here are two ways to do it :

  • First 13 way (the good way) : You subclass your UICollectionViewCell, and 12 give the subclass an "imageView" property. Then 11 you do this in your CustomCollectionViewCell.m 10 file :

    // Lazy loading of the imageView
    - (UIImageView *) imageView
        if (!_imageView) {
            _imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
            [self.contentView addSubview:_imageView];
         return _imageView;
    // Here we remove all the custom stuff that we added to our subclassed cell
        [super prepareForReuse];
        [self.imageView removeFromSuperview];
        self.imageView = nil;

    Then in your ViewController your have 9 to declare the new class of your collectionViewCells 8 like this :

    [self.collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    It will ensure that the images 7 are correctly removed on reuse, plus it's 6 way easier to setup the cell in your collectionView 5 delegate.

  • Second way (The dirty way), you 4 remove the views every time you load a new 3 cell :

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView                 cellForItemAtIndexPath:(NSIndexPath *)indexPath
        for (UIView *subview in [cell.contentView subviews]) {
            [subview removeFromSuperview];
        if (question.picture != (id)[NSNull null]) {
            //add AsyncImageView to cell
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.clipsToBounds = YES;
            imageView.tag = IMAGE_VIEW_TAG;
            [cell.contentView addSubview:imageView];
            [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imageView];
            imageView.imageURL = [NSURL URLWithString:question.picture];

    This way is far easier but i wouldn't 2 recommend it :P

Now try this and let me know 1 how your bug evolves.

Score: 2

Code for Swift 4

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {   
    return 1   

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)

    var imageview:UIImageView=UIImageView(frame: CGRect(x: 50, y: 50, width: 200, height: 200));

        var img : UIImage = UIImage(named:"Your image name")
        imageview.image = img



    return cell

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSize(width: 50, height: 414)


More Related questions