У меня есть горизонтальный прокрутки 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)
}
Что происходит в функции sd_setImageWithURL (URL)? Это расширение? – Luke
Да, это SD Web Image. –