如何在UICollection View单元中显示图像,同时单击swift ios中的单元格?

问题描述:

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索引路径中执行此操作吗?如何在UICollection View单元中显示图像,同时单击swift ios中的单元格?

//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图像。

+0

你必须使用didSelectRowAtIndexPath方法方法 –

+0

你说的意思是“我在收集观察单元的图像。我只需要显示图像时,我点击单元格。”? –

+0

@NiravD其实它是一个“嘀嗒”的图像。当我点击collectionview中的一个单元格时,该tickmark图像应该只显示在该单元格上。 –

声明一个IndexPath类型的实例并保持其单元状态为selected或不与它。现在使用cellForItemAt indexPathdidSelectItemAt indexPath这个数组。

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() 
} 
+0

我必须在我选择的单元格上显示它。现在它显示在我选择的所有单元上。 –

+0

@RakeshMohan你想在一次只有一个单元格的选择? –

+0

是的。我只需要在我选择的单元格中显示该图像。 –

您需要阉羊蜱在cellForRow

选择或不读,如果打勾选择了{ showImage }其他{ hideImage }

然后在didSelectRow 设置打勾选择或取消选中 然后调用reloadRowForIndexPath

+0

你能告诉我如何在我的代码中执行它吗? –