簡體   English   中英

如何在集合視圖單元格中更改圖像視圖

[英]How to change Image view in collection view cell

我以這種方式在集合視圖單元格中創建了一個imageView

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

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
    let imageView: UIImageView = UIImageView(image: UIImage(named: "image"))
    cell.contentView.addSubview(imageView)

    return cell
}

集合視圖有2個單元格,現在我需要將第一個單元格的圖像更改為Batman ,第二個單元格更改為Superman ,我應該如何實現它?

您需要比較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
}

創建UICollectionViewCell的子類

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

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

並在viewDidLoad方法中

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

首先,您不應在cellForRowAtIndexPath中添加子視圖。 這些代碼應該是您自定義單元格的創建。

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
}

你可以嘗試這個代碼,我希望這對你有用

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

謝謝大家的答案! 但上面的答案要么有點復雜,要么不適合我,所以我給出了自己的解決方案,受到@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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM