Swift: UICollectionViewCell didSelectItemAtIndexPath Change backgroundColor

Swift: UICollectionViewCell didSelectItemAtIndexPath Change backgroundColor

Related searches

I'm easily able to change the background color of a cell in the CellForItemAtIndexPath method

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath     indexPath: NSIndexPath) -> UICollectionViewCell {
 cell.backgroundColor = UIColor.blackColor()
 }

However, when I attempt to change the color in the DidSelectItemAtIndexPath it does not work.

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

}

Also I read somewhere that using didSelectItemAtIndexPath won't work because once the collection view begins scrolling the color will change back

What is the fix in Swift?

Thank you so much for your help


You can use this method for that:

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){

    var cell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
    cell.backgroundColor = UIColor.magentaColor()
}

ios, I'm easily able to change the background color of a cell in the CellForItemAtIndexPath method func collectionView(collectionView: UICollectionView,� ios,swift It's crashing because your handler object is getting released and deallocated right after the call to handler.sendMessage(), and then a delegate callback is attempted on that now-deallocated object when you try to send or hit cancel.


Old question, but it can help someone:

You can't simply modify the cell, since your changes will be lost when you scroll your UICollectionView, or even worst, other cells could appear with a wrong background, because they'll be reused.

So, the best way to do that is create an array of NSIndexPath and append your selected indexPaths:

var selectedIndexes = [NSIndexPath]() {
    didSet {
        collectionView.reloadData()
    }
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
    // ...
    if let indexSelecionado = selectedIndexes.indexOf(indexPath) {
        selectedIndexes.removeAtIndex(indexSelecionado)
    } else {
        selectedIndexes.append(indexPath)
    }
}

// ...

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    // ...
    if self.selectedIndexes.indexOf(indexPath) == nil {
        cell.backgroundColor = UIColor.whiteColor() // Unselected
    } else {
        cell.backgroundColor = UIColor.redColor() // Selected
    }
    return cell
}

Changing cell background color in UICollectionView in Swift, I'm easily able to change the background color of a cell in the CellForItemAtIndexPath method func collectionView(collectionView: UICollectionView,� you can use border change on didSelectItemAtIndexPath override event like the below code and assign new settings on the cell.. Swift 3.x: override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { addToList.append(objectsArray[indexPath.row]) let cell = collectionView.cellForItem(at: indexPath) cell?.layer.borderWidth = 2.0 cell?.layer.borderColor


You can override UICollectionViewCell isSelected . It will apply changes in selected method.

class ButtonCollectionCell: UICollectionViewCell {

            override var isSelected: Bool{
                didSet{
                    if self.isSelected
                    {
                       self.layer.backgroundColor =  colorLiteral(red: 0.8058760762, green: 0.2736578584, blue: 0.1300437152, alpha: 1)

                    }  else
                    {
                       self.layer.backgroundColor =  colorLiteral(red: 0, green: 0, blue: 0, alpha: 0)

                    }
                }
            }

    }

iOS, Changing cell background color in UICollectionView in Swift func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath:� Overview. You can use UICollection View Cell as-is or subclass it to add additional properties and methods. The layout and presentation of cells is managed by the collection view and its corresponding layout object.


cell.backgroundColor = UIColor.redColor()

let startTime = DateUtils.getDispatchTimeByDate(NSDate(timeIntervalSinceNow: 0.1))
dispatch_after(startTime, dispatch_get_main_queue()) {[weak self] in
    //there is the code redirect to other viewcontroller
    openOtherVC()
}

Changing the Appearance of Selected and Highlighted Cells, In your MyViewController.swift add the following collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath backgroundColor = UIColor. swift uicollectionviewcell delete. ios – Swift: UICollectionViewCell didSelectItemAtIndexPath Change backgroundColor – Stack Overflow. UICollectionView Tutorial Part 2: Reusable Views and Cell Selection – Ray Wenderlich. Animating Inserts and Deletes on UICollectionView with Swift – DZone Mobile. ios – Set up a UICollectionView delete


Swift; Objective-C This sample app shows how to change the appearance of collection view For example, the sample app uses the selectedBackgroundView property to change the cell's background color from red to blue when selecting the cell. func collectionView(_ collectionView: UICollectionView, didSelectItemAt� Create UICollectionView programmatically. Set UICollectionView layout UIEdgeInsets. Set UICollectionView item size (width and height) Set UICollectionView background color. Set UICollectionView Item background color. Implement UICollectionView Delegate method didSelectItemAtIndexPath to handle an event when user taps on one of the collection view items.


As just an example we can set the background color of selected cell to green. Objective-C-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath]; cell.backgroundColor = [UIColor greenColor]; } Swift


With the image view still selected, change its Mode to Aspect Fit in the Attributes inspector, so the images are not cropped or stretched in any way: Subclassing UICollectionViewCell. UICollectionViewCell doesn’t allow for much customization beyond changing the background color. You will almost always want to create your own subclass to allow