2017-03-03 7 views
0

こんにちは、私は私のUICollectionViewに問題があります。まず、私は、URL画像を含むサーバーから来る画像のリストを持っています。Swift Collection無作法の画像null null

しかし、URLがnullのとき、セルがランダムに画像を見せています。 URLがnullの場合は、画像が表示されません。

私はUIImageViewに画像を表示するためSDWebImageを使用しています。 UICollectionViewCellとして

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

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! MyCollectionListViewCell 
     if let url = self.listData[indexPath.row]["photo"] as? String { 
      let urlPhoto = NSURL(string:url!) 
      cell.imageView.sd_setImageWithURL(urlPhoto, placeholderImage:img) 
      cell.imageView.contentMode = UIViewContentMode.ScaleAspectFill 
      cell.imageView.layer.masksToBounds = true 
      cell.imageView.clipsToBounds = true 
     } 
} 

答えて

1

だけセルのnilに画像を設定し、再利用されます:それは動作します

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! MyCollectionListViewCell 
     if let url = self.listData[indexPath.row]["photo"] as? String { 
      let urlPhoto = NSURL(string:url!) 
      cell.imageView.sd_setImageWithURL(urlPhoto, placeholderImage:img) 
      cell.imageView.contentMode = UIViewContentMode.ScaleAspectFill 
      cell.imageView.layer.masksToBounds = true 
      cell.imageView.clipsToBounds = true 
     } else { 
      cell.imageView.image = nil 
     } 
} 
+0

感謝を... ..! – Voyager

関連する問題