2016-08-09 7 views
0

Ich habe eine horizontale Scroll-UICollectionView in einem View-Controller. Wenn die Ansicht gescrollt wird, verschwinden die Bilder in der Ansicht. Ich habe versucht, mich ohne Erfolg auf die Wiederverwendung vorzubereiten. Ich habe den Code unten geschrieben:UICollectionViewCell Remote Image verschwindet auf Scroll

-Sammlung Ansicht Handy

import UIKit 
import SDWebImage 

class StickerCell: UICollectionViewCell { 
    @IBOutlet weak var stickerImage: UIImageView! 

    override func prepareForReuse() { 
     super.prepareForReuse() 

     self.stickerImage.image = nil 
    } 

    func setImage(image: String) { 
     let url = NSURL(string: image) 
     self.stickerImage.contentMode = .ScaleAspectFit 
     self.stickerImage.clipsToBounds = true 
     self.stickerImage.center = self.center 
     self.stickerImage.sd_setImageWithURL(url) 
    } 
} 

View Controller

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return self.stickers.count 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell: StickerCell = collectionView.dequeueReusableCellWithReuseIdentifier("stickerCell", forIndexPath: indexPath) as! StickerCell 

    let sticker = stickers[indexPath.row] 

    if let image = sticker["images"]["medium"].string { 
     cell.setImage(image) 
    } 

    return cell 
} 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    var cell = collectionView.cellForItemAtIndexPath(indexPath) 
    if cell?.selected == true { 
     let sticker = stickers[indexPath.row] 
     if let image = sticker["images"]["medium"].string { 
      let url = NSURL(string: image) 
      self.stickerView.contentMode = .ScaleAspectFit 
      self.stickerView.clipsToBounds = true 
      self.stickerView.sd_setImageWithURL(url) 
     } 
    } 
} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    return CGSizeMake(115, 115) 
} 
+0

Was ist in der sd_setImageWithURL los (url) Funktion? Ist das eine Erweiterung? – Luke

+0

Ja, es ist SD Web Image. –

Antwort

0

die Sie interessieren .. -> Entfernen setImage Methode von strickerCell Klasse. -> Implementieren dieser

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell: StickerCell = collectionView.dequeueReusableCellWithReuseIdentifier("stickerCell", forIndexPath: indexPath) as! StickerCell 
let sticker = stickers[indexPath.row] 

if let image = sticker["images"]["medium"].string { 
    let url = NSURL(string: image) 
    cell.stickerImage.contentMode = .ScaleAspectFit 
    cell.stickerImage.clipsToBounds = true 
    cell.stickerImage.sd_setImageWithURL(url, placeholderImage:placeHolderImage, completed: { (image, error, cacheType, url) -> Void in 
    cell.stickerImage.image = image 
    }) 
} 

    return cell 
} 
+0

Das habe ich ursprünglich gemacht und das gleiche Problem verursacht. –

+0

Eigentlich, egal, das hat funktioniert! Vielen Dank! –