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

Posted

技术标签:

【中文标题】如何更改集合视图单元格中的图像视图【英文标题】:How to change Image view in collection view cell 【发布时间】:2016-08-05 06:17:02 【问题描述】:

我以这种方式在集合视图单元格中创建了一个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

collection view有2个cell,现在我需要把第一个cell的图片改成Batman,第二个改成Superman,应该怎么实现呢?

【问题讨论】:

检查索引路径并设置图片 不要在collectionView(collection:cellForRowAtIndexPath:) 中向UICollectionViewCell 添加子视图。细胞被重复使用。 【参考方案1】:

首先你不应该在 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

【讨论】:

我似乎无法通过 cell.imageView.image 访问图像视图 你应该继承 UICollectionViewCell 并在那里创建 UIImageView。如果您在 cellForItemAtIndexPath 中有 addSubview 则子视图将在单元格中添加多次【参考方案2】:

你需要比较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")

【讨论】:

我要更换图片,如果我再添加subview,会不会添加太多图片? 不,您的单元格只会重复使用 所以你需要创建UICollectionViewCell子类并实现prepareForReuse并在其中添加uiimageView【参考方案3】:

你可以试试这段代码,希望对你有用

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

【讨论】:

Xcode 说:无法转换“UIView”类型的值?到“UIImageView!” @BrightFuture 我已经更新了我的答案。请看一下【参考方案4】:

谢谢大家的回答!但是上面的答案要么有点复杂,要么不适合我,所以我给出了自己的解决方案,受到 @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

【讨论】:

以上是关于如何更改集合视图单元格中的图像视图的主要内容,如果未能解决你的问题,请参考以下文章

如何通过单击更改集合视图单元格中的标签颜色

如何从集合视图中的不可见单元格中删除自定义视图

将数组中的图像嵌入到集合视图的单元格中

集合视图单元格中的动态按钮宽度

如何使用分页更改 tableview 自定义单元格中的 imageview 图像。

如何在单元格中调用图像 url 和图像视图以获取集合视图的索引路径功能