2017-02-27 6 views
0

私は画像のグリッドを表示するコレクションビューを持っています。それは、ユーザーが自分自身に電子メールを送るために3つまでの画像を選択することを可能にする。ユーザーがセル(イメージ)をタップすると、黄色にハイライトされ、ファイル名が配列に追加されます。再度タップすると、選択が解除され、ハイライトが削除され、イメージがアレイから削除されます。UIコレクションビューで選択したハイライト表示されたセルを消去する方法

ユーザーが電子メールを送信すると、MFMailComposeResultデリゲートを使用して配列からアイテムを削除しますが、セルから黄色のハイライトを削除する方法はわかりません。誰かが助けられるかもしれないと願っています。感謝します。

私はdidSelectItemAtとdidDelectlectItemAt関数でイメージのファイル名を追加します。電子メールがどのようにクリアするために、デリゲート

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

任意のアイデアを使用してコードをここに送られているしたら

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

で細胞を強調していますハイライトされたセル?

答えて

1

選択したインデックスパスごとに、コレクションビューでdeselectItem(at indexPath: IndexPath, animated: Bool)を呼び出すことをお勧めします。

Fortunatelly、UICollectionViewには、選択したインデックスパスを一覧表示するプロパティがあります。したがって、mailComposeController(_: didFinishWith:)では、次のように書くことができます。

if let indexPaths = collectionView.indexPathsForSelectedItems { 
    indexPaths.forEach { self.collectionView.deselectItem(at: $0, animated: false) } 
} 
+0

は完璧に機能しました。ありがとう! –

関連する問題