Now I want to play different sounds when I click buttons inside these table view cells. I am very new at programming and I couldn't figure out how to do this. I have created an array for my sound files but I don't know how to assign them to those buttons in a row. Thank you very much for your help
AND THIS IS MY CODE
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
}
}