使用Swift 3
我已经设置了一个collectionView与7节-当用户点击一个单元格(按钮)一个特定的声音将从声音数组播放。
那么我如何在collectionView中拆分部分以匹配特定的声音数组?
例如
1) sounddarray1中的声音将在用户点击collectionView
section 1中的单元格(按钮)时播放2)当用户点击collectionView
section 2中的单元格(按钮)时,sounddarray2中的声音将播放3)当用户点击collectionView
section 3中的单元格(按钮)时,sounddarray3中的声音将播放一直到第7节
下面是用户点击cellButton
时的当前代码@IBAction func cellButton(_ sender: AnyObject) {
let sound = (sender as! UIButton).tag
self.setupAudioPlayer(file: peopleSounds[sound] as NSString, type: ".m4a")
self.soundPlayer.play()
}
其中peopleSounds是collectionView中section 1的第一个声音数组它按顺序完美地工作-但这个peopleSounds数组目前也会被collectionView中的其他section调用
那么我现在如何使我的第二个数组的声音播放时,用户点击按钮在collectionView的第2节等直到第7节?
谢谢
将其设置为2d Array,并将section号存储在一个变量中。您将能够像这样访问文件名(一般代码):
// 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]
希望有帮助。