Change Background of Uicollectionview Cell on Tap

Change Background of UICollectionView Cell on Tap

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

how to change cell background colour on selected item in UICollectionView and change other Cell colour to default in swift

Create a variable inside the vc

var currentSelected:Int?

then inside cellForItemAt

cell.backgroundColor = currentSelected == indexPath.row ? UIColor.green : UIColor.clear

finally didSelectItemAt

currentSelected = indexPath.row
collectionView.reloadData()

Don't depend on isSelected as cells are dequeued and when you scroll you'll get unexpected results

Change Color of UICollectionViewCell on touch

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
}

Deselect and change background of other cells in collection view when one cell is selected

In your custom UICollectionViewCell, override isSelected property and change the backgroundColor based on selected state in didSet observer, i.e.

class CollectionViewCell: UICollectionViewCell {
override var isSelected: Bool {
didSet {
self.backgroundColor = isSelected ? .red : .white
}
}

//rest of the code...
}

No need to implement didSelectItemAt in this case.

How to reset background color of UICollectionVIewCell

I am assuming you are allowing multiple selections in your collectionView like this
collectionView.allowsMultipleSelection = true

To Select / Deselect cell you can override the UICollectionViewCell property isSelected

 override var isSelected: Bool{

didSet{
self.backgroundColor = isSelected ? SELECTEDCOLOR: UNSELECTEDCOLOR

}
}

To Get the selected Cell indexes :

let selectedItems = self.collectionView.indexPathsForSelectedItems

To Reset the Selected cell:

 for indexPath in selectedItems {
self.collectionView.deselectItem(at: indexPath, animated:true)
}

highlighting uicollectionview cell on tap

I had the exact same problem and the solution is actually much simpler then the one posted above.

In your view controller, add collectionView.delaysContentTouches = false.

And then your other code within the cell was fine as is:

class SlideOutMenuCells: UICollectionViewCell {

//Setup code...

override var isHighlighted: Bool {
didSet {
if self.isHighlighted {
backgroundColor = UIColor.green
} else {
backgroundColor = UIColor.red
}
}
}
}

But now that annoying delay is gone!

How do I change the background image of a UICollectionViewCell that has been tapped?

I totally agree with the Josh's answer, but if you change the background image using the didSelectItemAtIndexPath method it works fine as well. Then, you can use the cellForRowAtIndexPath method that returns the UITableViewCell at the specified indexPath, like in the following way:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {        
var cell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!
cell.backgroundView = UIImageView(image: UIImage(named: "photo2"))
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.backgroundView = UIImageView(image: UIImage(named: "photo1"))
cell.selectionStyle = .None
return cell
}

I just put the selectionStyle to .None to avoid the highlight. I hope this help you.



Related Topics



Leave a reply



Submit