如何在 swift ios 中单击该单元格时在 UICollection View 单元格中显示图像? [英] How to show an image in a UICollection View cell while clicking that cell in swift ios?

查看:31
本文介绍了如何在 swift ios 中单击该单元格时在 UICollection View 单元格中显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PlaceCollectionViewCell



    //let imageView = UIImageView()
    //cell.backgroundColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00)

    cell.layer.borderWidth = 0.5
    cell.layer.borderColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00).cgColor


   //cell.placeLabel.tintColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00).cgColor

    cell.layer.cornerRadius = 40
    cell.layer.masksToBounds = true


    print("places\(indexPath.row)")
    //cell.placeLabel.text = places[indexPath.row] as! String
    cell.placeLabel.text = places[indexPath.row]
    cell.placeLabel.textColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00)

    // code for VIew
    let view = UIView(frame: cell.bounds)
     //Set background color that you want
    view.backgroundColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00)
    cell.selectedBackgroundView = view



    return cell

我在集合视图单元格中有一个图像.我只需要在单击单元格时显示该图像.我们是否必须在 cellforRowat 索引路径中执行此操作?

I have an image in collection view cell. I need to display that image only when I click the cell.Do we have to do it in cellforRowat index path?

//Custom class code:


  @IBOutlet weak var tickImageView: UIImageView!

@IBOutlet weak var placeViewBtn: UIButton!
@IBOutlet weak var placeLabel: UILabel!

@IBOutlet weak var imageView: UIImageView!
override func layoutSubviews() {

}
   override func awakeFromNib() {

    self.imageView.layer.cornerRadius = self.imageView.frame.size.width/2

    self.imageView.layer.masksToBounds = true
    super.awakeFromNib()
    // Initialization code
}

添加了自定义类代码.我想显示我在上面声明的ticMark图像.

Custom class code added. I want to display the ticMark image which I have declared above.

推荐答案

声明一个 IndexPath 类型的实例并维护单元格状态是 selected 与否.现在在 cellForItemAt indexPathdidSelectItemAt indexPath 中像这样使用这个数组.

Declare one instance of type IndexPath and maintain the cell status is it selected or not with it. Now use this array within cellForItemAt indexPath and didSelectItemAt indexPath like this.

var selectedIndexPaths = IndexPath()

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PlaceCollectionViewCell
    //Your code 

    //decide weather to show tick image or not
    self.tickImageView.isHidden = self.selectedIndexPaths != indexPath
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {   
    if self.selectedIndexPaths == indexPath {
         self.selectedIndexPaths = IndexPath()
    }
    else {
         self.selectedIndexPaths = indexPath
    }
    self.tableview.reloadData()
}

这篇关于如何在 swift ios 中单击该单元格时在 UICollection View 单元格中显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆