0

i am using a collectionview inside a tablview cell, in which collectionview cells are having an imageview on which images are displaying from the array of URLs in cellForItemAtIndexPath. I am calling webservice inside tableview cell class to getting array of URLs. My problem is that whenever i scrolls down or up tableview, collectionview cells are repeating, images of collectionview are repeating itself. so how to get rid out of this.

My Code is as follows -

inside response of webservice

let imgList : NSArray = arrReponseDetails.value(forKey: "imageList") as! NSArray
        self.arrImgUrls.removeAll()
        for dict in imgList[0] as! NSArray {
            let dictionary = dict as! NSDictionary
            let strUrl : String = dictionary["url_highRes"] as! String
            let url : URL = URL(string: strUrl)!
            self.arrImgUrls.append(url)
        }
        self.collectionView.reloadData()

code inside of cellForItemAtIndexPath

let cell : CollectionCell =  collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CollectionCell

let url : URL = self.arrImgUrls[indexPath.row]
cell.imgSticker.sd_setShowActivityIndicatorView(true)
cell.imgSticker.sd_setIndicatorStyle(.gray)
cell.imgSticker.sd_setImage(with: url, placeholderImage: nil)
cell.imgSticker.contentMode = .scaleAspectFit
return cell
rmaddy
  • 314,917
  • 42
  • 532
  • 579
iNoob
  • 144
  • 1
  • 15
  • You should not call Webservice within cell class, It may get called every time cell load – Abhishek Sep 11 '17 at 07:32
  • actually each collectionview representing distinct set of Images , i am passing a unique key from ViewController class to that tableview cell from which i am able to call webservice and then i am getting an array of urls as i posted in question. it will become too heavy for app if i call both the service in a single viewcontroller class – iNoob Sep 11 '17 at 07:38
  • Use an image caching library to remove flickering and also Implement some logic so webservice will get called only once for each Row, Not like each time when you scroll – Abhishek Sep 11 '17 at 09:18

4 Answers4

2

I am not getting much insight from your question but i think problem is with caching of image with reusable cell.

Simply implement prepareForReuse() method in collectionView Cell class(CollectionCell).

override func prepareForReuse() {
    super.prepareForReuse()
    self.imgSticker = nil
}

Reference of prepareForReuse()

technerd
  • 14,144
  • 10
  • 61
  • 92
  • i have already added this. for now images are fluctuating means old cell's images are appeared for a while then new images replaces them. and this process is repeating as i scrolls tablview – iNoob Sep 11 '17 at 07:44
0

You need to clear it out the old content from the cell's view before setting new one as in some case some content might be left and that reported issue occurs.

func collectionView(collectionView: UICollectionView, 
      cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 
    {
        let cell : CollectionCell =  collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CollectionCell

        cell.imageView.image = nil;  //Remove the image (cache one)
    }

Hope this helps!!

Pooja Gupta
  • 313
  • 2
  • 9
0

Hi You should add the below code on CollectionCell class:

- (void)prepareForReuse
{
    [super prepareForReuse];
    [[self imgSticker] setImage:nil];
}

Note: It would be better if you can set the placeholder image also.

-1

I wouldn' use colelctionView inside tableView, it's really strange approach. I used to do it myself for the first time and it got me into trouble. Consider changing the logic for collectionView with custom layout...

But to answer your question, it's better to use this approach (check out this answer)

just set in the CellForRow the imageView method according to this answer:

https://stackoverflow.com/a/27712427/5774854

Dominik Bucher
  • 2,120
  • 2
  • 16
  • 25