maidi
maidi

Reputation: 3459

Change the data source at the index of the cell that was tapped

I want to change display an image and hide a label at the cell of my collectionView when it is tapped. But as cells are reused, there are other cells set to displaying the image at scrolling. How can I prevent at. I already startet setting the tag of the cell but I don't know how to go on.

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    println("user tapped on cell number \(indexPath.row)")

    let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCollectionViewCell

    cell.tag = indexPath.row

    if (cell.tag == 0) {
        one = true

            if (cell.myLabel.hidden) {
                cell.myLabel.hidden = false
                cell.MyImageView.image = nil

            }
            else {
                cell.myLabel.hidden = true
                cell.MyImageView.image = UIImage(named:"1")!
            }
    }

Upvotes: 1

Views: 68

Answers (1)

Lamour
Lamour

Reputation: 3030

you should try to use the selected property that the cell has, The selected state is toggled when the user lifts up from a highlighted cell. then you could set your condition around it.

    if cell?.selected == true {// do whatever you want}  else{// same do whatever you want}

I don't think you need to set the tag because you could use indexPath inside of that method.

Upvotes: 1

Related Questions