ios UICollectionView选择和取消选择

tjjdgumg  于 2023-04-13  发布在  iOS
关注(0)|答案(7)|浏览(291)

我有UICollectionView 2行10+单元格。默认情况下取消选择。当我点击它成为选定的,但当我再次点击不取消选择。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print(indexPath)

   let cell = collectionView.cellForItem(at: indexPath)

    let collectionActive: UIImageView = {
        let image=UIImageView(image: #imageLiteral(resourceName: "collectionActive"))
        image.contentMode = .scaleAspectFill
        return image
    }()

    let collectionInactive: UIImageView = {
        let image=UIImageView(image: #imageLiteral(resourceName: "collectionInactive"))
        image.contentMode = .scaleAspectFill
        return image
    }()

    if cell?.isSelected == true {
        cell?.backgroundView = collectionActive
    }else{
        cell?.backgroundView = collectionInactive
    }

}

如何解决这个问题?

ctehm74n

ctehm74n1#

在viewDidLoad()中

collectionView.allowsMultipleSelection = true;

后记我实现了这些方法

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCell
    cell.toggleSelected()
}

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCell
    cell.toggleSelected()
}

终于在我的课堂上

class MyCell : UICollectionViewCell {

    func toggleSelected ()
    {
        if (selected){
            backgroundColor = UIColor.redColor()
        }else {
            backgroundColor = UIColor.whiteColor()
        }
    }

}
4dc9hkyq

4dc9hkyq2#

适用于Swift 5 +

在viewDidLoad()中

collectionView.allowsMultipleSelection = true

后记我实现了这些方法

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath) as! MovieDetailsDateCollectionViewCell
    cell.toggleSelected()
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath) as! MovieDetailsDateCollectionViewCell
    cell.toggleSelected()
}

在TableView单元格类中

class MyCell : UICollectionViewCell {

    func toggleSelected ()
       {
        if (isSelected){
            backgroundColor = .red
           }else {
            backgroundColor = .white
           }
       }

}
vhmi4jdf

vhmi4jdf3#

如果您不想启用多重选择,而只想一次选择一个单元格,则可以使用以下委托:
如果选择了单元格,则取消选择所有单元格,否则,如果未选择单元格,则正常选择单元格。

func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
    let cell = collectionView.cellForItem(at: indexPath) as! CustomCell

    if cell.isSelected {
        collectionView.selectItem(at: nil, animated: true, scrollPosition: [])
        return false
    }
    return true
}
6rvt4ljy

6rvt4ljy4#

根据UICollectionView class doc,,您可以用途:

var selectedBackgroundView: UIView? { get set }

当单元格被选中时,您可以使用此视图为单元格提供自定义外观。当单元格被选中时,此视图位于backgroundView之上,contentView之后。
cellForItem(at indexPath: IndexPath) -> UICollectionViewCell?函数的示例中,您可以设置:

cell.backgroundView = collectionInactive
cell.selectedBackgroundView = collectionActive
fafcakar

fafcakar5#

如果单元格被选中,只需在shouldSelectItemAt委托和DispatchQueue.main.async { }块中设置cell.isSelected = false。因此,在执行shouldSelectItemAt之后,状态实际上很快就会更改为false
它可能看起来像一个黑客,但它实际上工作。

func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
  if let cell = collectionView.cellForItem(at: indexPath), cell.isSelected {
    DispatchQueue.main.async { // change the isSelected state on next tick of the ui thread clock
      cell.isSelected = false
      self.collectionView(collectionView, didDeselectItemAt: indexPath)
    }

    return false
  }

  return true
}

请让我知道如果你发现/知道任何缺点这样做。谢谢🙏

7kqas0il

7kqas0il6#

在iOS 14及更高版本中,您可以设置单元格的backgroundConfiguration属性。设置后,所有用于选择和取消选择的必要视觉效果都会自动工作。您可以使用预配置的配置之一,如下所示:

cell.backgroundConfiguration = .listSidebarCell()

...或从头开始创建UIBackgroundConfiguration对象。您也可以在应用之前更改预配置的配置。
更多信息:https://developer.apple.com/documentation/uikit/uibackgroundconfiguration

vql8enpb

vql8enpb7#

override var isSelected: Bool{
   didSet{
     if self.isSelected
       {
       //This block will be executed whenever the cell’s selection state is set to true (i.e For the selected cell)
       }
       else
       {
      //This block will be executed whenever the cell’s selection state is set to false (i.e For the rest of the cells)
       }
   }
}

把这个加到你的手机里
Source

相关问题