简体   繁体   English

Swift:UICollectionViewCell didSelectItemAtIndexPath 更改 backgroundColor

[英]Swift: UICollectionViewCell didSelectItemAtIndexPath Change backgroundColor

I'm easily able to change the background color of a cell in the CellForItemAtIndexPath method我可以轻松地在CellForItemAtIndexPath方法中更改单元格的背景颜色

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.但是,当我尝试更改DidSelectItemAtIndexPath的颜色时,它不起作用。

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另外我在某处读到使用didSelectItemAtIndexPath将不起作用,因为一旦集合视图开始滚动,颜色就会变回

What is the fix in Swift? 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()
}

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.不能简单地修改单元格,因为滚动UICollectionView时您的更改将丢失,或者更糟糕的是,其他单元格可能会出现错误的背景,因为它们将被重用。

So, the best way to do that is create an array of NSIndexPath and append your selected indexPaths:因此,最好的方法是创建一个NSIndexPath数组并附加您选择的 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
}

You can override UICollectionViewCell isSelected .您可以覆盖 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)

                    }
                }
            }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM