0

Using Swift 3

I have set up a collectionView with 7 sections - when user taps a cell(button) a specific sound will play from an array of sounds.

So how do I split up the sections in the collectionView to match a specific array of sounds?

for instance

1) sounds in soundArray1 will play when users tap cells(buttons) in section 1 of collectionView

2) sounds in soundArray2 will play when users tap cells(buttons) in section 2 of collectionView

3) sounds in soundArray3 will play when users tap cells(buttons) in section 3 of collectionView

all the way up to section 7.

Here is the current code when user taps cellButton

@IBAction func cellButton(_ sender: AnyObject) {
    let sound = (sender as! UIButton).tag
    self.setupAudioPlayer(file: peopleSounds[sound] as NSString, type: ".m4a")
    self.soundPlayer.play()
   }

where peopleSounds is my first array of sounds for section 1 in the collectionView which works perfectly in order - but this peopleSounds array is also called for every other section in the collectionView at the moment

so how do I now make my second array of sounds play when user taps buttons in section 2 of collectionView etc up to section 7?

thank you

1 Answers1

0

Make it a 2d Array and store the section number in a variable. You'll be able to access the file name like (Code in general):

// the 2d array:
let soundFiles = [[String]]()

// you should change its value by checking the selected cell's section
var currentSection = 0

// somewhere in your code, you can do:
let firstList = soundFiles[currentSection]
let soundFileName = firstList[buttonTag]

Hope that helped.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143