winston
winston

Reputation: 3100

Update CollectionView cells that are not selected?

I have a CollectionView that allows a user to touch a cell and it will change the border color. However, I only want one cell to be selected at a time. How can I edit this code so that the cell at indexpath is updated with the border color and previously selected cell is reset?

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

        self.user["avatar"] = self.avatars[indexPath.row]

        do {
           try self.user.save()
        } catch {
            print(error)
        }

        let cell = collectionView.cellForItemAtIndexPath(indexPath) as! AvatarViewCell
        cell.layer.borderWidth = 5.0
        cell.layer.borderColor = UIColor.purpleColor().CGColor

Thanks!

UPDATE

       let cell = collectionView.cellForItemAtIndexPath(indexPath) as! AvatarViewCell
        var previouslySelectedIndexPath: NSIndexPath?
        if previouslySelectedIndexPath != nil {
            let previousCell = collectionView.cellForItemAtIndexPath(previouslySelectedIndexPath!) as! AvatarViewCell
            previousCell.layer.borderWidth = 0
            previousCell.layer.borderColor = UIColor.whiteColor().CGColor
        }

        cell.layer.borderWidth = 5.0
        cell.layer.borderColor = UIColor.purpleColor().CGColor

Upvotes: 1

Views: 984

Answers (2)

Kristin Harkness
Kristin Harkness

Reputation: 82

You could implement

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath)

Here's an example using a vanilla UICollectionViewCell:

// MARK: UICollectionViewDelegate

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

    if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
        cell.layer.borderWidth = 5.0
        cell.layer.borderColor = UIColor.purpleColor().CGColor
    }
}

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

    if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
        cell.layer.borderWidth = 0
        cell.layer.borderColor = UIColor.whiteColor().CGColor
    }
}

Upvotes: 2

paulvs
paulvs

Reputation: 12053

Why don't you have an instance variable (add at the beginning of your class file) to store the previously selected cell

var previouslySelectedIndexPath: NSIndexPath?

Then every time a new cell is selected, you first remove the border from the previously selected cell and then add the border to the newly selected cell

if previouslySelectedIndexPath != nil {
    let previousCell = collectionView.cellForItemAtIndexPath(previouslySelectedIndexPath!) as! AvatarViewCell
    previousCell.borderWidth = 0
}
let currentCell = collectionView.cellForItemAtIndexPath(indexPath) as! AvatarViewCell
cell.layer.borderWidth = 5.0
cell.layer.borderColor = UIColor.purpleColor().CGColor
previouslySelectedIndexPath = indexPath

Upvotes: 2

Related Questions