Reputation: 11
this is my did select item code
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
playAudioFile(sound: dArray[indexPath.row].sound)
dArray[indexPath.row].image = UIImage(#imageLiteral(resourceName: "backicon"))
collectionView.reloadData()
}
and this is my array from where am taking data:
struct Data {
let tittle : String
var image : UIImage
let sound : String
}
var dArray : [Data] = [
Data (tittle: "Artboard", image:#imageLiteral(resourceName: "Artboard 1 copy 8"), sound:"nice"),
Data (tittle: "Baby Laugh", image:#imageLiteral(resourceName: "babylaugh"),sound:"nice"),
Data (tittle: "Bike Horn", image:#imageLiteral(resourceName: "bikehorn"),sound:"test"),
Data (tittle: "Bomb", image:#imageLiteral(resourceName: "bomb"),sound:"nice"),
Data (tittle: "Bonus", image:#imageLiteral(resourceName: "bonus"),sound:"nice"),
Data (tittle: "Bottle", image:#imageLiteral(resourceName: "bottle"),sound:"nice"),
Data (tittle: "Bounce", image:#imageLiteral(resourceName: "bounce"),sound:"nice"),
Data (tittle: "Break", image:#imageLiteral(resourceName: "brake"),sound:"nice"),
Data (tittle: "Broken Glass", image:#imageLiteral(resourceName: "brokenglas"),sound:"nice"),
Data (tittle: "Bubbles", image:#imageLiteral(resourceName: "bubbles"),sound:"nice"),
Data (tittle: "Bugle", image:#imageLiteral(resourceName: "bugle"),sound:"nice"),
Data (tittle: "Buzzing Tone", image:#imageLiteral(resourceName: "buzzzingtone"),sound:"nice"),
Data (tittle: "Car Horn", image:#imageLiteral(resourceName: "carhorn"),sound:"car_horn"),
Data (tittle: "Car Racing", image:#imageLiteral(resourceName: "carracing"),sound:"racing_f1_passing"),
Data (tittle: "Car Start", image:#imageLiteral(resourceName: "carstart"),sound:"car_start"),
Data (tittle: "Cash Register", image:#imageLiteral(resourceName: "cashregister"),sound:"cash_register"),
Data (tittle: "Choo Train", image:#imageLiteral(resourceName: "chootrain"),sound:"choo_choo"),
Data (tittle: "Coin Drop", image:#imageLiteral(resourceName: "coindrop"),sound:"coin_drop_sound"),
Data (tittle: "Communication", image:#imageLiteral(resourceName: "communication"),sound:"communicator"),
Data (tittle: "Computer Tone", image:#imageLiteral(resourceName: "computertone"),sound:"computer_error"),
Data (tittle: "Crowd", image:#imageLiteral(resourceName: "crowd"),sound:"crowd_cheer"),
Data (tittle: "Cuckoo", image:#imageLiteral(resourceName: "cuckoo"),sound:"cuckoo"),
Data (tittle: "Cuckoo Clock", image:#imageLiteral(resourceName: "cuckooclock"),sound:"cuckoo_clock"),
Data (tittle: "Discovery", image:#imageLiteral(resourceName: "discovery"),sound:"discovery"),
Data (tittle: "displeasure", image:#imageLiteral(resourceName: "displeasure"),sound:"nice"),
Data (tittle: "Doorbell", image:#imageLiteral(resourceName: "doorbell"),sound:"door_bell"),
Data (tittle: "dopppler Tone", image:#imageLiteral(resourceName: "dopplertone"),sound:"car_alarm"),
Data (tittle: "Drum", image:#imageLiteral(resourceName: "drum"),sound:"drums"),
Data (tittle: "Elephent", image:#imageLiteral(resourceName: "elephant"),sound:"elephant"),
Data (tittle: "Emergency", image:#imageLiteral(resourceName: "emergency"),sound:"emergency_alarm"),
Data (tittle: "Engine", image:#imageLiteral(resourceName: "engine"),sound:"engine_car_start"),
Data (tittle: "Error", image:#imageLiteral(resourceName: "error"),sound:"error"),
Data (tittle: "Explosive", image:#imageLiteral(resourceName: "explosive"),sound:"explosive_beats_text"),
Data (tittle: "Female Scream", image:#imageLiteral(resourceName: "femalescream"),sound:"screaming_girl"),
Data (tittle: "Flush", image:#imageLiteral(resourceName: "flush"),sound:"flush_from_toilet"),
Data (tittle: "Gong", image:#imageLiteral(resourceName: "gong"),sound:"gong"),
Data (tittle: "Gun", image:#imageLiteral(resourceName: "gun"),sound:"shot"),
Data (tittle: "Gun Silencer", image:#imageLiteral(resourceName: "gunsilencer"),sound:"gun_silencer"),
Data (tittle: "Gym Whistle", image:#imageLiteral(resourceName: "gymwhistle"),sound:"whistle"),
Data (tittle: "Horse", image:#imageLiteral(resourceName: "horse"),sound:"horse_sound"),
Data (tittle: "Hunting", image:#imageLiteral(resourceName: "hunting"),sound:"gun_reloaded"),
Data (tittle: "Instant Chat", image:#imageLiteral(resourceName: "instantchat"),sound:"message_tone"),
Data (tittle: "Jungle", image:#imageLiteral(resourceName: "jungle"),sound:"jungle_sound (1)"),
Data (tittle: "Karate", image:#imageLiteral(resourceName: "karate"),sound:"karate_scream"),
Data (tittle: "Kisses", image:#imageLiteral(resourceName: "kises"),sound:"kissing"),
Data (tittle: "Klaxon", image:#imageLiteral(resourceName: "klaxon"),sound:"klaxon"),
Data (tittle: "Knock Door", image:#imageLiteral(resourceName: "knockeddoor"),sound:"knock"),
Data (tittle: "Load Gun", image:#imageLiteral(resourceName: "loadgun"),sound:"gun_load"),
Data (tittle: "Rewind", image:#imageLiteral(resourceName: "rewind"),sound:"rewinding"),
Data (tittle: "Warning", image:#imageLiteral(resourceName: "warning"),sound:"warning_alert_msg"),
]
Upvotes: 1
Views: 817
Reputation: 21
You can add a variable in your Data Struct
struct Data {
let tittle : String
var image : UIImage
let sound : String
var isSelected : Bool
}
And change didSelect code to this
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
playAudioFile(sound: dArray[indexPath.row].sound)
//This condition is to select only one collectionView item at a time.
if dArray[indexPath.row].isSelected {
dArray[indexPath.row]. isSelected = false
} else {
dArray[indexPath.row]. isSelected = true
}
collectionView.reloadData()
}
And add this code in your cellForItem function
if dArray[indexPath.row].isSelected {
dArray[indexPath.row].image = UIImage(#imageLiteral(resourceName: "backicon"))
}
I think this helps. Thank you.
Upvotes: 1