不同的聲音現在我要玩不同的聲音,當我點擊這些表視圖細胞內的按鈕。我在編程方面非常新,我無法弄清楚如何做到這一點。我爲我的聲音文件創建了一個數組,但我不知道如何將它們分配給一行中的這些按鈕。非常感謝您的幫助
,這是我的代碼
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'類型的值 –