不同的声音现在我要玩不同的声音,当我点击这些表视图细胞内的按钮。我在编程方面非常新,我无法弄清楚如何做到这一点。我为我的声音文件创建了一个数组,但我不知道如何将它们分配给一行中的这些按钮。非常感谢您的帮助
,这是我的代码
import UIKit
import AVFoundation
class ViewController6: UIViewController, UITableViewDataSource, UITableViewDelegate {
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Array of Rifles
var arrayOfRifles = ["AK47", "AUGA1", "FAMAS", "G36K", "K1A", "K2", "M4A1", "M4Silencer", "M16", "M60", "MicroGALIL", "QBZ95", "RPK", "ScarHeavy", "Scarlight", "SG552", "TAR21", "Tommy", "Type89", "XM8"]
var buttonDataRifles = ["AK47", "AUGA1", "FAMAS", "G36K", "K1A", "K2", "M4A1", "M4Silencer", "M16", "M60", "MicroGALIL", "QBZ95", "RPK", "ScarHeavy", "Scarlight", "SG552", "TAR21", "Tommy", "Type89", "XM8"]
var soundsArray: [NSURL] = [
NSURL(fileURLWithPath: Bundle.main.path(forResource: "AK47", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "AUGA1", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "FAMAS", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "G36K", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "K1A", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "K2", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "M4A1", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "M4Silencer", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "M16", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "M60", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "MicroGALIL", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "QBZ95", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "RPK", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "ScarHeavy", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "ScarLight", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "SG552", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "TAR21", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "Tommy", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "Type89", ofType: "mp3")!),
NSURL(fileURLWithPath: Bundle.main.path(forResource: "XM8", ofType: "mp3")!)
]
//Functions for tableView
//Cell - For Rifles
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (arrayOfRifles.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell6 = tableView.dequeueReusableCell(withIdentifier: "cell6", for: indexPath) as! ViewControllerTableViewCell6
cell6.myImage.image = UIImage(named: arrayOfRifles[indexPath.row] + ".jpg")
cell6.myButton.setTitle(buttonDataRifles[indexPath.row], for: UIControlState.normal)
return (cell6)
cell6.myButton.tag = indexPath.row
cell6.myButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)}
@IBAction func buttonAction(_ sender: UIButton) {
print("Button tapped")
let tapedIndex = sender as UIButton
let getUrl = soundsArray[tapedIndex] as! NSURL
}
}
究竟是什么意思,“分配一些标签到该按钮,以确定哪个被点击”? –
我们必须在这里指定cell6.myButton.tag = indexPath.row作为标记,所以只要我们只点击一行按钮,所以我们可以识别哪个按钮单击我们可以播放该行的歌曲。 – Bucket
let getUrl = soundsArray [tapedIndex] as! NSURL 我在这一行发生错误,说'不能用'UIButton'类型的索引来下标'NSURL'类型的值 –