简体   繁体   English

如何在集合视图单元格中更改图像视图

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

I created an imageView in a collection view cell this way: 我以这种方式在集合视图单元格中创建了一个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
}

The collection view has 2 cells, now I need to change the image of the first cell to Batman and the second to Superman , how should I achieve it? 集合视图有2个单元格,现在我需要将第一个单元格的图像更改为Batman ,第二个单元格更改为Superman ,我应该如何实现它?

You need compare indexPath.row 您需要比较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
}

Create your subclass of UICollectionViewCell 创建UICollectionViewCell的子类

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

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

And in viewDidLoad method 并在viewDidLoad方法中

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

First of all you shouldn't add subviews in cellForRowAtIndexPath. 首先,您不应在cellForRowAtIndexPath中添加子视图。 These code should be in creation of your custom 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
}

You can try this code, I hope this works for you 你可以尝试这个代码,我希望这对你有用

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

Thank you all for the answers! 谢谢大家的答案! But the above answers are either a little complicated, or not working on my side, so I'm giving my own solution, inspired by @HDT : 但上面的答案要么有点复杂,要么不适合我,所以我给出了自己的解决方案,受到@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