使用Swift 2我將如何創建一個音頻文件數組以用於集合視圖?Swift:音頻文件數組
有4個選項「海洋」,「鳥類」,「自然」,「波浪」,它們顯示爲單元格。
當單擊單元格時,我需要它播放聲音。你能幫忙嗎?
var imageArray = [UIImage(named: "ocean"),UIImage(named: "birds"),UIImage(named: "nature"),UIImage(named: "wave")]
var nameArray = ["Ocean","Birds","Nature","Waves"]
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! homeCollectionViewCell
cell.iconImage?.image = imageArray[indexPath.item!]
cell.label.text = nameArray[indexPath.item!]
return cell
}
y在哪裏y你想播放音頻嗎?相同的'viewController'或導航到不同的? – Santosh
viewcontroller @Santosh –
您是否實現了'didSelectRowAtIndexPath'方法?如果沒有,那麼你應該使用它來執行任何操作來選擇'tableView'中的特定'cell'。因此,創建一個更多的音頻文件陣列,並在音頻文件數組中使用'indexPath.row'獲取音頻文件。 – Santosh