Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

uicollectionview - How to show an image in a UICollection View cell while clicking that cell in 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

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
}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...