CollectionView duplicate cell when loading more data

后端 未结 2 964
有刺的猬
有刺的猬 2020-12-10 03:54

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

相关标签:
2条回答
  • 2020-12-10 04:30

    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!)
      }
    }
    
    0 讨论(0)
  • 2020-12-10 04:41

    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 ...
    
    0 讨论(0)
提交回复
热议问题