func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
if let cell = collectionView.cellForItem(at: indexPath) {
cell.backgroundColor = UIColor.green
}
}
为什么点击手机时显示背景颜色大约需要45秒?还有其他办法吗?
答案 0 :(得分:1)
在扩展UICollectionViewCell的类上执行此操作:
override var isSelected: Bool {
didSet {
backgroundColor = isSelected ? UIColor.black : UIColor.blue
}
}
答案 1 :(得分:0)
只需删除此行
即可 self.collectionView?.reloadData()
就是这样:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath){
cell.backgroundColor = .green
}
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath){
cell.backgroundColor = .clear
}
}
答案 2 :(得分:0)
你也可以使用它,你可以用时间进行自定义
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath)
{
cell.backgroundColor = .green
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2 , execute:
{
cell.backgroundColor = .clear
})
}
}