如何在TableView中向UIButton添加音频?

时间:2018-06-14 18:13:39

标签: ios swift uitableview

使用Swift我一直试图弄清楚如何将单个音频文件添加到每个单元格中的每个UIButton。我只能使一个音频文件工作,但文件会附加到每个单元格中的每个按钮。这是我的代码:

import UIKit
import AVFoundation

public extension UITableView {
    func indexPathForView(_ view: UIView) -> IndexPath? {
        let origin = view.bounds.origin
        let viewOrigin = self.convert(origin, from: view)
        let indexPath = self.indexPathForRow(at: viewOrigin)
        return indexPath
    }
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, AVAudioPlayerDelegate {
    var audioPlayer: AVAudioPlayer!

    let elements = ["horse", "cat", "dog", "potato", "horse", "cat", "dog", "potato", "horse", "cat", "dog", "potato"]
    let animalSounds = ["horse1", "cat1", "dog1", "potato1", "horse2", "cat2", "dog2", "potato2", "horse3", "cat3", "dog3", "potato3"]

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        tableView.delegate = self
        tableView.dataSource = self

        super.viewDidLoad()
    }

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return elements.count
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! UITableTableViewCell

        cell.cellView.layer.cornerRadius = cell.cellView.frame.height / 2

        cell.animalLabel.text = elements[indexPath.row]
        cell.animalImage.image = UIImage(named: elements[indexPath.row])
        cell.animalImage.layer.cornerRadius = cell.animalImage.frame.height / 2
        cell.animalImage.clipsToBounds = true
        cell.playAnimalSound.actions(forTarget: "playAnimalNoises", forControlEvent: UIControlEvents.allEditingEvents)
        cell.playAnimalNoises.button
        return cell
    }

    @IBAction func playAnimalNoises(_ sender: UIButton) {
        //let indexPath = self.tableView.indexPathForView(sender) //worked as sender
        //playSounds(soundFileName: animalSounds)
        print("Button pressed @ ")
    }

    func playSounds(soundFileName: String){
        let soundURL = Bundle.main.url(forResource: soundFileName, withExtension: "wav")
        do {
            try audioPlayer = AVAudioPlayer(contentsOf: soundURL!)
        }
        catch {
            print("Somethings wrong...")
        }
        audioPlayer.play()
    }
}

// button shown in each cell,

如果您需要我进一步解释,请告诉我。

1 个答案:

答案 0 :(得分:0)

首先,您需要在单元格的按钮中正确添加目标/操作。

卸下:

cell.playAnimalSound.actions(forTarget: "playAnimalNoises", forControlEvent: UIControlEvents.allEditingEvents)
cell.playAnimalNoises.button

并添加:

cell.playAnimalSound.addTarget(self, action: #selector(playAnimalNoises), for: .touchUpInside)

然后更新playAnimalNoises

@objc func playAnimalNoises(_ sender: UIButton) {
    let point = self.tableView.convert(sender.bounds.origin, from: sender)
    let indexPath = self.tableView.indexPathForRow(at: point)
    playSounds(soundFileName: animalSounds[indexPath.row])
}