Swift: UICollectionViewCell didSelectItemAtIndexPath Change backgroundColor

雨燕双飞 提交于 2019-12-21 02:42:19

问题


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


回答1:


You can use this method for that:

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

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



回答2:


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
}



回答3:


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)

                    }
                }
            }

    }



回答4:


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()
}


来源:https://stackoverflow.com/questions/28596859/swift-uicollectionviewcell-didselectitematindexpath-change-backgroundcolor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!