【发布时间】:2017-08-16 21:27:05
【问题描述】:
我目前有一个 CollectionView 和 1 个可重复使用的单元格,其中填满了 ImageView。这个单元格被使用了 10 次来展示 10 种不同的动物(我提供了单元格的图片)。我在这个集合视图上方也有一个标签。每次用户按下单元格时,标签都会变为“所选动物的名称”。 我现在想要的是让单元格图像在被选中时变暗,在未选中时恢复正常。到目前为止,我知道如何将单元格的图像更改为更暗的图像手动将其切换为较暗的,但我不知道如何将其更改为正常,因为我选择了另一个。 我该如何实现?我在底部复制了我的代码:
//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
}
【问题讨论】:
标签: ios swift swift3 uicollectionview