Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何在UI集合视图中清除选定的高亮显示单元格_Swift_Uicollectionview_Xcode8.2 - Fatal编程技术网

Swift 如何在UI集合视图中清除选定的高亮显示单元格

Swift 如何在UI集合视图中清除选定的高亮显示单元格,swift,uicollectionview,xcode8.2,Swift,Uicollectionview,Xcode8.2,我有一个集合视图,它显示一个图像网格。它允许用户选择最多三个图像以通过电子邮件发送给自己。当用户点击单元格(图像)时,它会高亮显示黄色,文件名会添加到数组中;如果用户再次点击它,它会取消选择,高亮显示将被删除,图像将从数组中删除 一旦用户发送电子邮件,我就使用MFMailComposeResult委托从数组中删除项目,但我不知道如何从单元格中删除黄色突出显示。希望有人能帮忙。谢谢 我正在didSelectItemAt和didSelectItemAt函数中添加图像的文件名 func collect

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

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

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

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()     
    }
}

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

对于每个选定的索引路径,您需要在集合视图上调用
取消选择项(位于indexPath:indexPath,动画:Bool)

幸运的是,
UICollectionView
有一个列出所选索引路径的属性。因此,在
mailComposeController(uu:didFinishWith:)
中,您可以编写:

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

不如Tomah优雅的解决方案,但您可以

collectionView.allowsSelection = false
collectionView.allowsSelection = true
它将清除选择