2

I have an array of cells in a collection view that I want to get to act like buttons.

When I tap on one, I want that cell to highlight, and then change colors.

I'm able to initially set the color of the view.backgroundColor inside of the collectionViewCell us the cellForItemAtIndexPath method. However, contrary to what I thought would work if I do this:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let cell: ButtonCollectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("ButtonCell", forIndexPath: indexPath) as! ButtonCollectionCell
    cell.cellBackgroundView.backgroundColor = UIColor.darkGrayColor()
}

the color still isn't changing...

Essentially I want these cells to behave exactly like a button. I want them to highlight to a lighter gray color upon touch (they are initially white) after I release my finger I want them to become a dark gray color.

If I touch again I want them to again highlight to a lighter gray color, and then become white again.

If didSelectItemAtIndexPath isn't the way to do it, what is?

thelaws
  • 7,991
  • 6
  • 35
  • 54
YichenBman
  • 5,011
  • 8
  • 46
  • 65

4 Answers4

2

I would recommend some changes in your code that will help you to resolve your error.

First I would call the cellForItemAtIndexPath method to get your cell instead of using the dequeue method:

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

Then you should call the reloadItemsAtIndexPaths inside the didSelectItemAtIndexPath method to reload the cell:

collectionView.reloadItemsAtIndexPaths([indexPath])

Also you shouldn't change the background in the didSelectItemAtIndexPath method, but in the cellForItemAtIndexPath method where you check if the cell is selected:

if(cell?.selected){
  //set your background-color
}else{
  //change color
}
Christian
  • 22,585
  • 9
  • 80
  • 106
  • the reloadItemsAtIndexPaths and change of cellForItemAtIndexPath worked in so much that I can change the color of the cell when I press on it if the background is changed within the didSelectItemAtIndexPath.. it is no working with the `if(cell.selected) statement. The cells don't respond to anything I tell them to do in the if cell selected statement – YichenBman Feb 19 '15 at 21:43
1

You can implement your own touch down gesture recognizer for the UICollectionView. See https://stackoverflow.com/a/15629234/1557276

When it's done, call -indexPathForItemAtPoint: method of the UICollectionView instance and then make changes in the cell returned by -cellForItemAtIndexPath:

Community
  • 1
  • 1
Vlad Fedoseev
  • 152
  • 1
  • 11
1

When you dequeueReusableCellWithReuseIdentifier it can return a new cell to you.

Instead use cellForItemAtIndexPath to get the current cell at that index path.

thelaws
  • 7,991
  • 6
  • 35
  • 54
  • I'm using the `dequeue` method because I'm running into an optional unwrapping nil error when I use `cellForItemAtIndexPath`. Any ideas how to fix this? @thelaws – YichenBman Feb 19 '15 at 19:33
  • 1
    Presumably this is because `cellForItemAtIndexPath` will return `nil` for cells that are offscreen. You just need to properly check if the Optional is nil before trying to set the background colour of the cell. – thelaws Feb 19 '15 at 21:15
0

Put this line of code I your method:

collectionView.cellForItem(at: indexPath)?.backgroundColor = UIColor.cyan
Rocky Balboa
  • 784
  • 10
  • 25