作者:多米音乐_34249295 | 来源:互联网 | 2023-05-29 12:37
我正在使用水平集合视图来滚动日期.集合视图包含30个单元格.如果我选择第一个单元格以指示选择,则单元格背景颜色已从默认颜色红色变为棕色.然后,如果我选择另一个单元格,则选定的单元格颜色已从红色变为棕色.但是第一个细胞BGColor保持不变(棕色).如何通过单击其他单元格更改为默认颜色?
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath
indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell",
forIndexPath: indexPath) as myViewCell
cell.date_label.text = arr_date[indexPath.item]
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath
indexPath: NSIndexPath) {
var cell = collectionView.cellForItemAtIndexPath(indexPath) as myViewCell
if(cell.selected)
{
cell.backgroundColor = UIColor.brownColor()
}
else
{
cell.backgroundColor = UIColor.redColor()
}
}
Khalil Bello..
19
您可以将该函数collectionView
与参数一起使用didSelectItemAtIndexPath
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath
indexPath: NSIndexPath) {
let selectedCell:UICollectiOnViewCell= myCollectionView.cellForItemAtIndexPath(indexPath)!
selectedCell.contentView.backgroundColor = UIColor(red: 102/256, green: 255/256, blue: 255/256, alpha: 0.66)
}
这会为所选内容创建一个常量UICollectionViewCell
,然后您只需更改背景的颜色
然后,为了在取消选择时返回原始颜色,必须使用collectionView
带参数的函数didDeselectItemAtIndexPath
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
let cellToDeselect:UICollectiOnViewCell= myCollectionView.cellForItemAtIndexPath(indexPath)!
cellToDeselect.contentView.backgroundColor = UIColor.clearColor()
}
并将颜色更改为原始颜色!
例如,这里是filterApp中此代码的屏幕截图
UICollectionView示例
1> Khalil Bello..:
您可以将该函数collectionView
与参数一起使用didSelectItemAtIndexPath
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath
indexPath: NSIndexPath) {
let selectedCell:UICollectiOnViewCell= myCollectionView.cellForItemAtIndexPath(indexPath)!
selectedCell.contentView.backgroundColor = UIColor(red: 102/256, green: 255/256, blue: 255/256, alpha: 0.66)
}
这会为所选内容创建一个常量UICollectionViewCell
,然后您只需更改背景的颜色
然后,为了在取消选择时返回原始颜色,必须使用collectionView
带参数的函数didDeselectItemAtIndexPath
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
let cellToDeselect:UICollectiOnViewCell= myCollectionView.cellForItemAtIndexPath(indexPath)!
cellToDeselect.contentView.backgroundColor = UIColor.clearColor()
}
并将颜色更改为原始颜色!
例如,这里是filterApp中此代码的屏幕截图
UICollectionView示例
2> Abirami Bala..:
var selectedIndex = Int ()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
if selectedIndex == indexPath.row
{
cell.backgroundColor = UIColor.green
}
else
{
cell.backgroundColor = UIColor.red
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
selectedIndex = indexPath.row
self.yourCollctionView.reloadData()
}
可能很疯狂,但它对我来说很好......!