【问题标题】:Collectionview deselect selcted row when selecting new row in swift在 swift 中选择新行时,Collectionview 取消选择所选行
【发布时间】:2015-02-19 09:04:36
【问题描述】:

我正在 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()

【问题讨论】:

    标签: ios swift xcode6 uicollectionview


    【解决方案1】:

    将此代码转换为 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];
    
    }
    

    【讨论】:

      【解决方案2】:

      这正是您所需要的。将其设置在 cellForItemAtIndexPath 中,您无需关心何时取消选择以手动执行。

      func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
          //...
              var bgColorView = UIView()
              bgColorView.backgroundColor = UIColor.whiteColor()
              cell.selectedBackgroundView = bgColorView
      
              return cell
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-30
        • 1970-01-01
        • 2017-03-17
        • 1970-01-01
        • 1970-01-01
        • 2015-05-19
        • 2017-03-31
        相关资源
        最近更新 更多