私は現在CollectionView
に再利用可能なセルが1つあり、ImageView
で埋め尽くされています。この細胞は10回使用され、10種類の動物が表示されます(私は細胞の画像を提供します)。このコレクションビューの上にラベルがあります。ユーザがセルを押すたびに、ラベルは "動物の名前が選択されました"に変わります。 私が今したいのは、選択されたときに細胞イメージが暗くなり、選択されていないときに正常に戻ることです。現時点では、暗いもので手動で切り替えることで、セルの画像を暗くする方法を知っていますが、別のものを選択すると元の状態に戻す方法はわかりません。 これはどのように実装しますか?私は一番下に私のコードをコピーしている:これを行うためにはCollectionViewの選択に基づいてImageViewを変更する
//cell configuration
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//define the cell
let cell = collectionview.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! AnimalSelectionCell
//animals is name of array of different types of animals
cell.animalImage.image = UIImage(named: animals[indexPath.row])
cell.animalImage.restorationIdentifier = animals[indexPath.row]
return cell
}
//choosing an animal
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let currentCell = collectionview.cellForItem(at: indexPath) as! AnimalSelectionCell
let animal = currentCell.animalImage.restorationIdentifier! //gets what type of animal selected
selectionLabel.text = "\(animal) selected" //changes label
currentCell.animalImage.image = UIImage(named: animal + "Selected") //changes to darker animal image
}
これは、選択された動物にはより暗い画像を与えるが、別の動物を選択すると、以前選択した動物はまだ暗くなる。これをどうやって解決するのですか? – fphelp
また、明確なボタンがあり、それを押すとラベルは「選択された動物がありません」に変わります。このコードのどの部分をIBActionコードで使用する必要があるのですか?クリアボタンを押すと、すべての画像が正常に戻りますか? – fphelp
@fphelp私の答えを確認してください私は最後のrequerimentのために必要な実装でclearSelection関数を追加しました –