Swift:UICollectionViewCell didSelectItemAtIndexPath更改backgroundColor [英] Swift: UICollectionViewCell didSelectItemAtIndexPath Change backgroundColor
本文介绍了Swift:UICollectionViewCell didSelectItemAtIndexPath更改backgroundColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以轻松地在CellForItemAtIndexPath方法中更改单元格的背景颜色
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()
}
但是,当我尝试更改DidSelectItemAtIndexPath中的颜色时,它不起作用。
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()
}
另外我在某处读到使用didSelectItemAtIndexPath将无法正常工作,因为一旦集合视图开始滚动颜色将改变回来
Also I read somewhere that using didSelectItemAtIndexPath won't work because once the collection view begins scrolling the color will change back
什么是Swift中的修复?
What is the fix in Swift?
非常感谢你的帮助
推荐答案
你可以使用这个方法:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
var cell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
cell.backgroundColor = UIColor.magentaColor()
}
这篇关于Swift:UICollectionViewCell didSelectItemAtIndexPath更改backgroundColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文