2017-10-27 4 views
1

UICollectionView私はUIImageViewを含む再利用可能なセルを持っていますが、コレクションビューは、コレクション内の各セルアイテムの画像ビューに同じ画像を表示します。再利用可能なUICollectionViewCellはUIImageViewプロパティを変更していません(重複を示しています)

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let path = indexPath.item 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "friendAvatar", for: indexPath) as! AvatarCollectionViewCell 
    cell.avatarView.image = friends[path].userAvatar 
    return cell 

} 


class AvatarCollectionViewCell: UICollectionViewCell { 


    var avatarView = UIImageView() 


    override init(frame: CGRect) { 
     super.init(frame: frame) 

     prepareForReuse() 
     config() 

    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 


    override func prepareForReuse() { 

     avatarView.image = nil 
     avatarView.removeFromSuperview() 

    } 


    func config() { 

     avatarView.frame = CGRect(x: 0, y: 0, width: 64, height: 64) 
     avatarView.layer.cornerRadius = 64/4 
     avatarView.clipsToBounds = true 
     avatarView.contentMode = .scaleAspectFill 
     addSubview(avatarView) 

    } 


} 
+0

'let path = indexPath.row' – Guardanis

+0

@Guardanisの行はテーブルビューです。彼はコレクションビューを使用しています –

答えて

0

まず第一に、あなたはprepareForReuse()にそのスーパービューから画像表示を削除しているが、あなたはそれを再度追加することはありません。 init()はセルを再利用するときには呼び出されず、いずれもconfig()にはなりません。特別な理由がない限り、スーパービューからイメージビューを削除する必要はありません。そのイメージをnilに設定するだけで十分です。

また、ブレークポイントを設定し、friends[path].userAvatarが画像への有効なパスを返すことを確認します。

関連する問題