Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Background of UICollectionView Cell on Tap

I have a UICollectionView that I have created programmatically. I would like for the collection view to behave in the following way:

1. User touches cell 2. Cell background color changes 3. User releases touch 4. Cell background color changes 

This should be a quick color change that happens just before the selector related to the tap action is executed in which the viewcontroller containing the collection view is popped off the stack.

I have been looking at this question: UICollectionView cell change background while tap

in which there is the following summary of methods to use for this purpose:

// Methods for notification of selection/deselection and highlight/unhighlight events. // The sequence of calls leading to selection from a user touch is: // // (when the touch begins) // 1. -collectionView:shouldHighlightItemAtIndexPath: // 2. -collectionView:didHighlightItemAtIndexPath: // // (when the touch lifts) // 3. -collectionView:shouldSelectItemAtIndexPath: or -    collectionView:shouldDeselectItemAtIndexPath: // 4. -collectionView:didSelectItemAtIndexPath: or -collectionView:didDeselectItemAtIndexPath: // 5. -collectionView:didUnhighlightItemAtIndexPath: 

I am assuming I only need to implement one of the above methods from 'when touch begins' and 'when touch ends.' But no matter what I do, it appears that a background color changes and then remains changed. Here is an example of something I attempted which did not work:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath  {    //pop vc   }  - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {   UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];   cell.contentView.backgroundColor = [UIColor redColor]; }  - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {   UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];   cell.contentView.backgroundColor = [UIColor greenColor]; } 

This results in the cell background color being changed only to red. I also looked at this question: UICollectionView Select and Deselect issue and tried implementing [UICollectionView selectItemAtIndexPath:animated:scrollPosition:] and calling it inside of didSelectItemAtIndexPath, but this did not work either. Collection view data source and delegate are set.

like image 216
jac300 Avatar asked Apr 30 '14 15:04

jac300


People also ask

How do I highlight cells in collection view?

How to use? Just inherit BaseCollectionViewCell . If needed, configure in cell's init or collectionView 's delegate methods. If you don't need highlight effect, just find a method named 'shouldHighlightItemAtIndexPath' in UICollectionViewDelegate and return false or just set cell.

What is UICollectionViewDelegate?

The methods adopted by the object you use to manage user interactions with items in a collection view.

Can a uicollectionview cell change background color while tap?

Cell background color changes This should be a quick color change that happens just before the selector related to the tap action is executed in which the viewcontroller containing the collection view is popped off the stack. I have been looking at this question: UICollectionView cell change background while tap

How do I change the cell’s background color when selected?

For example, the sample app uses the selectedBackgroundView property to change the cell’s background color from red to blue when selecting the cell. Providing a selected background view in a cell is an easy way to have the collection view change the cell’s appearance based on its state, but you can do more than just changing the background.

What happens when you set the selectedbackgroundview to a view?

Setting the selectedBackgroundView to a view causes the collection view to replace the default background with the selected background when highlighting or selecting a cell. Your app doesn’t need to do anything else. The collection view changes the cell’s visual appearance automatically as the cell’s state changes.

How does the collection view change the cell’s visual appearance?

The collection view changes the cell’s visual appearance automatically as the cell’s state changes. For example, the sample app uses the selectedBackgroundView property to change the cell’s background color from red to blue when selecting the cell.


2 Answers

The problem is that you are changing the color on highlight and changing it back on deselect instead that on unhighlight

You should simply change this:

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {   UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];   cell.contentView.backgroundColor = [UIColor greenColor]; } 

to this:

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {   UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];   cell.contentView.backgroundColor = [UIColor greenColor]; } 

Also, if you don't want to wait a bit before getting your highlight happen you should set the delaysContentTouches property of the collection view to NO

Edit: also ensure that you call

[collectionView deselectItemAtIndexPath:indexPath animated:NO]; 

inside the -didSelectItemAtIndexPath method

like image 167
Antonio E. Avatar answered Oct 01 '22 19:10

Antonio E.


Swift 3 version

Add the following two methods to your view controller class:

// change background color when user touches cell func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {     let cell = collectionView.cellForItem(at: indexPath)     cell?.backgroundColor = UIColor.red }  // change background color back when user releases touch func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {     let cell = collectionView.cellForItem(at: indexPath)     cell?.backgroundColor = UIColor.green } 

See here for help in setting up a basic collection view in Swift.

like image 27
Suragch Avatar answered Oct 01 '22 19:10

Suragch