Praveen Kumar
Praveen Kumar

Reputation: 567

Change ImageView image on UICollectionViewCell on Tap

I have a UICollectionView which uses custom xib file for it's cells. In the cell I have an ImageView for check box. I want to change the image of the `ImageView1 when it was taped. I tried the following code snippet but, it's not working for me

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! V_Cell

    if show_delete == true {
        cell.img_delete.image = UIImage(named: "checked")
    }
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! V_Cell

    // Configure the cell

    let data = valves_[indexPath.row]
    cell.v_name.text = data

    if show_delete == true {
        cell.img_delete.isHidden = false
    } else if show_delete == false {
        cell.img_delete.isHidden = true
    }

    return cell
}

In this code I've tried to change the image of ImageView in didSelectItemAt.

My custom xib file is as following.

enter image description here

Please suggest a way to make it work. Thanks.

Upvotes: 0

Views: 1098

Answers (2)

KrishnaCA
KrishnaCA

Reputation: 5695

For these kind of problems, it is always better to keep an array which contains whether an item at a given indexpath is checked or not.

var checkArray: [Bool] = [Bool](repeating: false, count: numberOfRowsInCollectionView)
// this should be the same size as number of items in collection view

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! V_Cell
    checkArray[indexPath.row] = !checkArray[indexPath.row] // if it's true, make it false and vice versa logic
    collectionView.reloadItems(at: [indexPath])
}

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! V_Cell

    // Configure the cell

    let data = valves_[indexPath.row]
    cell.v_name.text = data

    if show_delete == true {
        cell.img_delete.isHidden = false
    } else if show_delete == false {
        cell.img_delete.isHidden = true
    }

    if checkArray[indexPath.row] {
        cell.img_delete.image = //image for check
    }else {
        cell.img_delete.image = //image for no check
    }

    return cell
}

Upvotes: 1

Praveen Kumar
Praveen Kumar

Reputation: 567

Finally I solved it by the following code.

    var checkArray = [Int]()

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if show_delete == true {
        if checkArray.contains(indexPath.row) {
            let index = checkArray.index(of: indexPath.row)
            checkArray.remove(at: index!)
            collectionView.reloadItems(at: [indexPath])
        } else {
            checkArray.append(indexPath.row)
            collectionView.reloadItems(at: [indexPath])
        }
    }
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! V_Cell

    // Configure the cell
    let data = valves_[indexPath.row]
    cell.v_name.text = data

    if show_delete == true {
        cell.img_delete.isHidden = false
    } else if show_delete == false {
        cell.img_delete.isHidden = true
    }

    if checkArray.contains(indexPath.row) {
        cell.img_delete.image = UIImage(named: "checked_n")
    } else {
        cell.img_delete.image = UIImage(named: "unchecked")
    }

    return cell
}

I put taped element indexes in an array and reloaded the items at indexpath. if the array contains the reloaded index then it displays the check mark, if not it removes the check mark.

Upvotes: 0

Related Questions