2016-08-09 3 views
0

У меня есть горизонтальный прокрутки UICollectionView в контроллере вида. Всякий раз, когда просматривается представление, изображения в представлении исчезают. Я пытался подготовиться к повторному использованию без успеха. Я отправил код ниже:Удаленное изображение UICollectionViewCell исчезает при прокрутке

Collection Посмотреть сотовый

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

Что происходит в функции sd_setImageWithURL (URL)? Это расширение? – Luke

+0

Да, это SD Web Image. –

ответ

0

Попробуйте это .. -> Удалить метод setImage из класса strickerCell. -> Реализовать это

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

На самом деле, неважно, что это сработало! Благодаря! –

Смежные вопросы