[英]Collectionview deselect selcted row when selecting new row in swift
我正在快速執行一個示例collectionView項目。它垂直顯示10行。我更改了選定行的背景顏色。如何在collectionview中選擇新行時取消選擇選定行的背景顏色
碼:
for indexPath in collectionView .indexPathsForSelectedItems(){
collectionView .deselectItemAtIndexPath(indexPath as? NSIndexPath, animated:false)
}
let Cell = collectionView.cellForItemAtIndexPath(indexPath)
Cell?.backgroundColor = UIColor .orangeColor()
將此代碼轉換為swift,您將獲得結果:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor magentaColor];
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor greenColor];
}
這正是您所需要的。 將其設置在cellForItemAtIndexPath內,您無需擔心手動選擇取消選中它的時間。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//...
var bgColorView = UIView()
bgColorView.backgroundColor = UIColor.whiteColor()
cell.selectedBackgroundView = bgColorView
return cell
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.