ビューコントローラに水平スクロール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)
}
sd_setImageWithURL(url)関数では何が起こっていますか?これは拡張機能ですか? – Luke
はい、SD Web画像です。 –