The problem:
I have a CollectionView which loads a UIimage into each cell. However my problem is that when I load additional cells with more images
in your custom cells setupData()
method try the following:
func setupData(){
self.imageView.image = nil // reset the image
if let urlString = data{
let url = NSURL(string: "http://image.tmdb.org/t/p/w342/" + urlString)
self.imageView.hnk_setImageFromURL(url!)
}
}
This is a typical table view/collection view setup issue.
Any time you recycle a cell using a dequeue method like dequeueReusableCellWithReuseIdentifier:
, you must always fully configure all the views in the cell, including setting all the text fields/image views to their starting values. Your code has several if statements where, if the condition of the if is false, you don't set up the views in the cell. You need to have else clauses that clear out old content from the cell's views in case there is content left over from the last time the cell was used.
**EDIT:
Change your cellForItemAtIndexPath
method to begin like this:
func collectionView(collectionView: UICollectionView,
cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell =
collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier,
forIndexPath: indexPath) as! UpcomingCollectionViewCell
cell.imageView.image = nil; //Remove the image from the recycled cell
//The rest of your method ...