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

时间:2016-08-05 06:17:02

标签: ios swift

我以这种方式在集合视图单元格中创建了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,我应该如何实现它?

4 个答案:

答案 0 :(得分: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
}

创建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")
}

答案 1 :(得分:0)

首先,您不应在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
}

答案 2 :(得分: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

答案 3 :(得分: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
}