【问题标题】:UICollectionView Cell with tick box in the image图像中带有复选框的 UICollectionView 单元格
【发布时间】:2015-07-30 00:22:58
【问题描述】:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {

        var cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell
        println(indexPath)
        cell.imageView.contentMode = .ScaleAspectFill
        cell.backgroundColor = UIColor.whiteColor()
        cell.layer.borderColor = UIColor.blackColor().CGColor
        cell.layer.borderWidth = 0.1
        cell.frame.size.width = self.screenWidth / 3
        cell.frame.size.height = self.screenWidth / 3


        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        let documentsDirectory = paths[0] as! String
        cell.imageView.image = UIImage(contentsOfFile: self.getMediaFilePath(self.mediaModels[indexPath.row].pathToMedia))
        cell.imageTicked.hidden = true

        return cell

    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 1
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){

        var cell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
        //cell.backgroundColor = UIColor.magentaColor()
        self.mediaModels[indexPath.row].isSelected  = true
        cell.selected = true
    }
    func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath){

        var cell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
        //cell.backgroundColor = UIColor.whiteColor()
        self.mediaModels[indexPath.row].isSelected  = false
        cell.selected = false
    }

我可以在返回单元格的函数中隐藏 imageTicked,但我无法将其添加到 didSelectItemAtIndexPathdidDeselectItemAtIndexPath 中。 我想在选择项目时显示复选框,并在取消选择项目时隐藏复选框。

只是想知道如何在didDeselectItemAtIndexPath 内设置cell.imageTicked.hidden = true 和在didSelectItemAtIndexPath 内设置cell.imageTicked.hidden = false

非常感谢您的帮助

【问题讨论】:

    标签: ios iphone swift ios8


    【解决方案1】:

    您只需要创建一个 selectedIndexPath 变量。在 didSelectItemAtIndexPath 中设置它并在 didDeselectItemAtIndexPath 中删除。记得调用 reloadData() 来重新加载集合视图。

    var selectedIndexPath: NSIndexPath?;
    ...         
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath: NSIndexPath){
       ...
       if selectedIndexPath != nil {
          cell.imageTicked.hidden = selectedIndexPath.row == indexPath.row
       }
       else {
          cell.imageTicked.hidden = true
       }
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
       ...
       self.selectedIndexPath = indexPath
       collectionView.reloadData()
    }
    
    func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath){
      ...
      self.selectedIndexPath = nil
      collectionView.reloadData()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-29
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-02
      • 1970-01-01
      相关资源
      最近更新 更多