Reputation: 731
I have a collection view and each cell has a button, I added an IB-action that is called when the button is pressed.
My issue is that when a certain button is tapped I want to change not only the background color of that button, I want to change all buttons in all the cells
I'm not sure how to implement this...
Thanks.
Upvotes: 1
Views: 48
Reputation: 9503
Try Somthg like this
Take a var selectedIndex : Int = -1
in cellForItemAt
cell.button.tag = indexpath.item
if selectedIndex == indexPath.item{
cell.button.backgroundColor = UIColor.blue // New Color
}
else{
cell.button.backgroundColor = UIColor.gray // Deafult color
}
in Button Action
selectedIndex = sender.tag
collectionView.ReloadData()
Upvotes: 1
Reputation: 6018
When you pressed to the button you should notify your collectionView class holder (by delegate
pattern or using a closure
) that right now all buttons should be changed. Then you should force change all visible buttons (take all visibleCells
of collectionView and change the buttons style) and also in the cellForItemAtIndexPath
method change the button style for every other buttons which is invisible right now.
Upvotes: 0