2017-07-24 108 views
1

!我需要你的幫助。我有UICollectionView裏面的UIImageView裏面的UITableView。當我第一次從API獲取數據時,它會顯示正確的圖像,但是當我開始向下滾動並返回時,它會顯示錯誤的圖像。我的代碼如下所示:在UICollectionView中顯示錯誤圖像

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AllPostsOfUserCollectionViewCell 

    let post = allPostsOfUserArray[collectionView.tag] 
    if post.imageLinks.count != 0 { 
     let imageLink = post.imageLinks[indexPath.row] 

     if imageLink.imageLink != nil { 
      let url = URL(string: imageLink.imageLink!) 

      cell.imageOfAnimalInCollectionView.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in 
      }) 
     } 
    } 

    return cell 
} 

我的模型看起來是這樣的:

class UserContent { 

var name = "" 
var imageLinks = [UserImages]() 

init(name: String, imageLinks: [UserImages]?) {   
    self.name = name 
    if imageLinks != nil { 
     self.imageLinks = imageLinks! 
    } 
} 

init() { } 

deinit { 
    imageLinks.removeAll() 
    } 
} 


class UserImages { 

var imageLink: String? 

init(imageLink: String?) { 
    self.imageLink = imageLink 
} 

init() { } 

deinit { 
    imageLink = "" 
    } 
} 

我的UITableView做什麼

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let index = indexPath.row 

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! AllPostsOfUserTableViewCell 

    cell.collectionView.tag = index 

    let post = allPostsOfUserArray[index] 
    if post.imageLinks.count == 0 { 
     cell.collectionView.isHidden = true 
    } else { 
     cell.collectionView.isHidden = false 
    } 

    return cell 
} 

UPD:我加cell.collectionView.reloadData () to func tableView(cellForRowAt indexPath)。現在它工作正常。

+0

在所有'else'(你沒有做的)中,你需要刪除圖片或放置一個佔位符。 – Larme

回答

0
cell.imageOfAnimalInCollectionView.image = nil 
if post.imageLinks.count != 0 { 
    let imageLink = post.imageLinks[indexPath.row] 

    if imageLink.imageLink != nil { 
     let url = URL(string: imageLink.imageLink!) 

     cell.imageOfAnimalInCollectionView.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in 
     }) 
    } 

} 

(未測試,但應該工作。你也可以使用sd_image的方法來分配一個空的網址,用一個佔位符圖像只顯示圖像佔位符,或者只是給一個空的URL字符串''無佔位符圖像)

無論何時滾動,單元格都會重新使用,並且分配給單元格的圖像會重新出現,因爲它仍在單元格上。 您必須通過使用else子句來刪除這些圖像,其中每個if您正在將圖像分配給imageView。

希望這會有所幫助。

+0

謝謝,但我仍然有一些麻煩。我想,也許這是因爲錯誤的模型。 –

+0

@DmytroRyshchuk很​​棒,你找到了解決方案。不過,我認爲將圖像設置爲零或佔位符圖像是一種很好的做法。 因爲你正在處理更多的數據 –

0

難道你試試這個代碼:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AllPostsOfUserCollectionViewCell 
    cell.imageOfAnimalInCollectionView.image = UIImage(named: "App-Default") 
    let post = allPostsOfUserArray[collectionView.tag] 
    if post.imageLinks.count != 0 { 
     let imageLink = post.imageLinks[indexPath.row] 

     if imageLink.imageLink != nil { 
      let url = URL(string: imageLink.imageLink!) 

      cell.imageOfAnimalInCollectionView.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in 
      }) 
     } 
    } 

    return cell 
} 
0

這個問題有點像競爭狀態。 您可以參考我的答案similair question

您可能需要找到另一個模塊下載圖像並手動設置爲單元格。