【问题标题】:How to clear selected highlighted cells in UI Collection View如何清除 UI 集合视图中选定的突出显示的单元格
【发布时间】:2017-07-17 12:58:58
【问题描述】:

我有一个显示图像网格的集合视图。它允许用户选择最多三个图像通过电子邮件发送给自己。当用户点击一个单元格(图像)时,它会突出显示黄色并将文件名添加到数组中,如果他们再次点击它会取消选择,突出显示将被移除,并且图像会从数组中移除。

用户发送电子邮件后,我使用 MFMailComposeResult 委托从数组中删除项目,但我不知道如何从单元格中删除黄色突出显示。希望有人可以提供帮助。谢谢。

我在 didSelectItemAt 和 didDeselectItemAt 函数中添加图像的文件名。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let fileName = filenames[indexPath.item]
    selectedFileNames.append(fileName)
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    let fileName = filenames[indexPath.item]
    if let index = selectedFileNames.index(of: fileName) {
        selectedFileNames.remove(at: index)
    }    
}

我正在突出显示 UICollectionViewCell 类中的单元格

override var isSelected: Bool {
    didSet {
        self.layer.borderWidth = 3.0
        self.layer.borderColor = isSelected ? UIColor.yellow.cgColor : UIColor.clear.cgColor
    }
} 

在此处发送电子邮件后,就是使用委托的代码

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    controller.dismiss(animated: true)
    if result == MFMailComposeResult.sent {
        print("emailed Photos")
        self.selectedFileNames.removeAll()
        self.fullSizeSharableImages.removeAll()     
    }
}

知道如何清除突出显示的单元格吗?

【问题讨论】:

    标签: swift uicollectionview xcode8.2


    【解决方案1】:

    对于每个选定的索引路径,您需要在集合视图上调用 deselectItem(at indexPath: IndexPath, animated: Bool)

    幸运的是,UICollectionView 具有列出所选索引路径的属性。所以,在mailComposeController(_: didFinishWith:),你可以写:

    collectionView.indexPathsForSelectedItems?
        .forEach { self.collectionView.deselectItem(at: $0, animated: false) }
    

    【讨论】:

      【解决方案2】:

      不如 tomahh 优雅的解决方案,但您可以调用

      collectionView.allowsSelection = false
      collectionView.allowsSelection = true
      

      它会清除选择

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多