2016-08-09 7 views
0

ビューコントローラに水平スクロールUICollectionViewがあります。ビューがスクロールされるたびに、ビュー内のイメージは消えます。私は成功して再利用の準備をしようとしました。私は以下のコードを掲載しました:UICollectionViewCellスクロールでリモートイメージが消える

コレクションビューセル

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) 
    } 
} 

をビューコントローラ

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

sd_setImageWithURL(url)関数では何が起こっていますか?これは拡張機能ですか? – Luke

+0

はい、SD Web画像です。 –

答えて

0

これを試してみてください。.. - > strickerCellクラスからsetImageメソッドを削除します。 - >これを実装する

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

これはもともと私がやったことであり、同じ問題を引き起こしました。 –

+0

実際、これはうまくいきません!ありがとう! –

関連する問題