H.Epstein
H.Epstein

Reputation: 731

collectionViewCells interact

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

Answers (2)

dahiya_boy
dahiya_boy

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

pacification
pacification

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

Related Questions