匹配不同阵列的声音文件的每个各部分使用夫特3SWIFT 3:的CollectionView
我已经设置了的CollectionView与7个部分 - 当用户敲击细胞(按钮)特定声音将从播放声音阵列。
那么,我该如何拆分collectionView中的部分以匹配特定的声音数组?
例如
1)听起来soundArray1将起到当用户点击细胞(按钮)在的CollectionView
2)第1声音在soundArray2将起到当用户在第2挖掘细胞(按钮)的CollectionView
3)的声音在soundArray3将发挥在用户中的CollectionView第3拼接单元(按钮)
一路攀升至第7
这里是当前代码时用户轻敲cellButton
func numberOfSections(in collectionView: UICollectionView) -> Int {
return sections.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return self.appleProducts.count
} else if section == 1 {
return self.animals.count
} else if section == 2 {
return self.food.count
} else if section == 3 {
return self.activity.count
} else if section == 4 {
return self.travel.count
} else if section == 5 {
return self.objects.count
} else if section == 6 {
return self.symbols.count
} else {
return 0
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
if indexPath.section == 0 {
cell.cellButton.setTitle(appleProducts[indexPath.row], for: UIControlState.normal)
}
else if indexPath.section == 1 {
cell.cellButton.setTitle(animals[indexPath.row], for: UIControlState.normal)
}
else if indexPath.section == 2 {
cell.cellButton.setTitle(food[indexPath.row], for: UIControlState.normal)
}
else if indexPath.section == 3 {
cell.cellButton.setTitle(activity[indexPath.row], for: UIControlState.normal)
}
else if indexPath.section == 4 {
cell.cellButton.setTitle(travel[indexPath.row], for: UIControlState.normal)
}
else if indexPath.section == 5 {
cell.cellButton.setTitle(objects[indexPath.row], for: UIControlState.normal)
}
else if indexPath.section == 6 {
cell.cellButton.setTitle(symbols[indexPath.row], for: UIControlState.normal)
}
cell.cellButton.tag = indexPath.row
return cell
}
@IBAction func cellButton(_ sender: AnyObject) {
let sound = (sender as! UIButton).tag
self.setupAudioPlayer(file: Sounds[sound] as NSString, type: ".m4a")
self.soundPlayer.play()}
其中声音是声音文件阵列1。然而,这个声音文件阵列现在正在collectionView的所有部分中播放。我无法解决如何分割每个声音阵列以匹配collectionView中的每个部分。
使用'indexPathForItem(在点:CGPoint) - > IndexPath?'(使用按钮的'centre'作为点),然后使用'indexPath.section'确定从哪个数组中选择(使用'indexPath.row ')。 – pbasdf
似乎没有一个函数indexPathForItem?最接近的是didSelectItemAt indexPath –
请参阅[这里](https://developer.apple.com/reference/uikit/uicollectionview/1618030-indexpathforitem)。 – pbasdf