如何清除UI集合视图中选定的突出显示单元格

Jos*_*een 2 uicollectionview swift xcode8.2

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

一旦用户发送电子邮件,我使用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)
    }    
}
Run Code Online (Sandbox Code Playgroud)

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

override var isSelected: Bool {
    didSet {
        self.layer.borderWidth = 3.0
        self.layer.borderColor = isSelected ? UIColor.yellow.cgColor : UIColor.clear.cgColor
    }
} 
Run Code Online (Sandbox Code Playgroud)

一旦发送电子邮件,这里是使用代表的代码

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()     
    }
}
Run Code Online (Sandbox Code Playgroud)

知道如何清除突出显示的细胞吗?

tom*_*ahh 9

对于每个选定的索引路径,您都希望调用deselectItem(at indexPath: IndexPath, animated: Bool)集合视图.

Fortunatelly,UICollectionView有一个列出所选索引路径的属性.所以,mailComposeController(_: didFinishWith:)你可以写:

collectionView.indexPathsForSelectedItems?
    .forEach { self.collectionView.deselectItem(at: $0, animated: false) }
Run Code Online (Sandbox Code Playgroud)