2016-11-14 9 views
0

私はテーブルビューで別のガンを表示するアプリを持っています。私はテーブルビューのセルにボタンを持っています。私はそれらのボタンをタップすると、銃の音を再生したい。オーディオファイルの配列を作成しました。そして私は進歩を遂げたと信じていますが、私は自分が望むものを達成できませんでした。ここにあなたの助けをテーブルビューのセルでボタンをタップすると、配列内のサウンドを再生する

をありがとう、私は、この行でエラーを取得する私のコード

import UIKit 
import AVFoundation 

class ViewController6: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    //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 soundArrayRifles = ["AK47", "AUGA1", "FAMAS", "G36K", "K1A", "K2", "M4A1", "M4Silencer", "M16", "M60", "MicroGALIL", "QBZ95", "RPK", "ScarHeavy", "Scarlight", "SG552", "TAR21", "Tommy", "Type89", "XM8"] 

    var audioPlayer: AVAudioPlayer = AVAudioPlayer() 

    func setupAudioPlayerWithFile(file: NSString, type: NSString) -> AVAudioPlayer? { 

     let path = Bundle.main.path(forResource: file as String, ofType: type as String) 
     let url = NSURL.fileURL(withPath: path!) 

     var audioPlayer : AVAudioPlayer? 

     do { 
      try audioPlayer = AVAudioPlayer(contentsOf: url) 
     } catch { 
      print("Player not available") 
     } 
     return audioPlayer 

    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


//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 

    } 


    @IBAction func buttonAction(_ sender: UIButton) { 
     let range: UInt32 = UInt32(soundArrayRifles.count) 
     //FIND OUT WHICH SOUND HERE 
     let sound = self.setupAudioPlayerWithFile(soundArrayRifles, type: "mp3") 
     sound.play() 

    } 

    } 

です。それは私はあなたがそれへの参照を持っているので、ボタンタグであなたのindexPath.rowを保存しようとここに

let sound = self.setupAudioPlayerWithFile(soundArrayRifles, type: "mp3") 
+0

私は 'setupAudioPlayerWithFile'は配列全体ではなく最初の引数として文字列を取ることを意味すると思います –

+0

あなたは何か提案がありますか? –

答えて

0

「をsoundArrayRifles」配列を追加することはできません。あなたのボタンアクションよりも

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) 
    cell6.myButton.tag = indexPath.row 
    return cell6 
} 

このような何か:

@IBAction func buttonAction(_ sender: UIButton) { 
    let soundIndex = sender.tag 
    //FIND OUT WHICH SOUND HERE 
    let sound = self.setupAudioPlayerWithFile(soundArrayRifles[soundIndex], type: "mp3") 
    sound.play() 

} 

それはテストされていないが、それはあなたがそれをする必要が何をすべきを。

+0

ありがとうございます。もうエラーはありません。しかし、私はアプリをビルドすると、それが開きますが、私はボタンをタップするとクラッシュします。この行に何か問題があります。 'let url = NSURL.fileURL(withPath:path!)' –

+0

あなたのパスが良くないことを意味します。 index = 0を考えて、あなたのサウンドパスは "AK47.mp3"でなければなりません。そのパスが有効でない場合は、応答としてnilを取得し、NSURL.fileURL(withPath:path!)を呼び出すと、! - クラッシュしないようなものを巻き戻します。 –

+0

私は申し訳ありませんが、私はあなたが意味することをかなり理解していませんでした –

0
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) 
    cell6.myButton.tag = indexPath.row 
    cell6.myButton.addTarget(self, action: #selector(ViewController6.buttonAction(sender:)), for: .touchUpInside) 
    return cell6 
    } 

    func buttonAction(_ sender: UIButton) { 

      let sound = self.setupAudioPlayerWithFile(soundArrayRifles[sender.tag], type: "mp3") 
      sound.play() 

    } 
+0

ボタンをタップするとアプリケーションがクラッシュする( –

+0

セル内のボタンがIBActionにリンクされていないことを確認してください –

+0

ボタンをViewController6に直接追加したと思いますが、カスタム行のカスタムクラスを追加する必要があります。ボタンをカスタムクラスに追加する – aahmetbas

関連する問題