2016-08-05 13 views
1

コレクションビューセル内の画像表示を変更するにはBatmanと2番目の数字はSupermanですが、どうすればよいですか?どのように私はコレクションビューのセルにこの方法を<code>imageView</code>を作成

+0

チェックindexpathとサブビューを追加しないでください画像 –

+1

を設定しますcollectionView(collection:cellForRowAtIndexPath:)の 'UICollectionViewCell'に渡します。セルは再利用されます。 – Larme

答えて

0

のために働く願う解答ありがとうございました!しかし、上記の答えは少し複雑、あるいは私の側に動作していないのどちらかであるので、私は@HDTに触発され、私自身の解決策を与えている:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) 

    if cell.contentView.subviews.count != 0 { 

     cell.contentView.subviews[0].removeFromSuperview() 

    } 

    switch indexPath.row { 
    case 0: 
     cell.contentView.addSubview(UIImageView(image: frontImage)) 
    case 1: 
     cell.contentView.addSubview(UIImageView(image: backImage)) 
    default: 
     cell.contentView.addSubview(UIImageView(image: UIImage(named: "Image"))) 
    } 

    return cell 
} 
0

あなたはUICollectionViewCell

class CustomCell: UICollectionViewCell { 
    @IBOutlet var imageView : UIImageView! 

    override func prepareForReuse() { 
     super.prepareForReuse() 
     imageView.image = nil 
    } 
} 

そしてviewDidLoad方法あなたがcellForRowAtIndexPathでサブビューを追加しないでくださいすべての

func viewDidLoad() { 
    collectionView.registerClass(CustomCell.self, forCellWithReuseIdentifier: "CustomCell") 
} 
+0

イメージを置き換える必要があります。サブビューをもう一度追加すると、あまりにも多くのイメージを追加しませんか? –

+0

いいえ、あなたのセルはちょうど再利用されます – iSashok

+0

'UICollectionViewCell'サブクラスを作成し、' prepareForReuse'を実装し、その中にuiimageViewを追加する必要があります – iSashok

0

まず中のサブクラスを作成しindexPath.row

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CustomCell 

    var image = UIImage(named: "Superman")! 
    if indexPath.row == 0 { 
     image = UIImage(named: "Batman")! 
    } 

    cell.imageView.image = image! 

    return cell 
} 

を比較する必要があります。これらのコードは、カスタムセルを作成するときに使用する必要があります。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) 

    switch(indexPath.row) { 
     case 1: cell.imageView.image = UIImage(named: "batman")! 
     case 2: cell.imageView.image = UIImage(named: "superman")! 
     default: cell.imageView.image = UIImage(named: "placeholder")! 
    } 
    return cell 
} 
+0

cell.imageView.imageによって画像ビューにアクセスできないようです。 –

+0

UICollectionViewCellからサブクラス化してそこにUIImageViewを作成する必要があります。 cellForItemAtIndexPathにaddSubviewがある場合、サブビューはセル内に何度も追加されます – Konstantin

0

あなたはこのコードを試すことができ、私はこれがあなた

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellIdentifier", forIndexPath: indexPath) 

     var imageView: UIImageView? = cell.contentView.viewWithTag(1111) as? UIImageView 

     if (imageView != nil) { 
      //switch/case, if/else whatever you want to set images accordingly 
      imageView!.image = UIImage(named: "image") 
     } else { 
      imageView = UIImageView() 
      imageView!.tag = 1111 
      cell.contentView.addSubview(imageView!) 
     } 

     return cell 
+0

Xcodeは言う: "UIView?"の値を変換できません。 「UIImageView!」へ –

+0

@BrightFuture私は自分の答えを更新しました。見てください – HDT

関連する問題