Swift 5.0の場合:
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.allowsMultipleSelection = true
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
(collectionView.indexPathsForSelectedItems ?? [])
.filter { $0.section == indexPath.section && $0.item != indexPath.item && $0.row != indexPath.row }
.forEach { self.collectionView.deselectItem(at: $0, animated: false) }
}
セクション内のアイテムを切り替える場合は、次を使用します。
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.allowsMultipleSelection = true
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
(collectionView.indexPathsForSelectedItems ?? [])
.filter { $0.section == indexPath.section && $0.item }
.forEach { self.collectionView.deselectItem(at: $0, animated: false) }
}
それはユーザーエクスペリエンスを向上させると思いますが、それはあなたの呼び出しです