Reputation: 142
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
Upvotes: 0
Views: 1112
Reputation: 91
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.
Upvotes: 0
Reputation: 333
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!!
Upvotes: 0
Reputation: 2167
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
Upvotes: -1
Reputation: 14514
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()
Upvotes: 2