I have a list of songs which should be played in each of row in table view by tapping on the button. I've made a button in custom cell and a protocol which used to display the tap in our view controller. For ex. when we tap on second button in cell, it should play second song, when we tap on third button, its play third song.
My ViewController:
import UIKit
import AVFoundation
class BeatPackViewController: UIViewController {
@IBOutlet weak var beatTableView: UITableView!
@IBOutlet weak var coverImage: UIImageView!
@IBOutlet weak var looppackNameLabel: UILabel!
@IBOutlet weak var producerNameLabel: UILabel!
@IBOutlet weak var backButtonLabel: UIButton!
let data = DataLoader().beatData
var songs: [String] = []
var audioPlayer = AVAudioPlayer()
func getSongs() -> [String] {
var names: [String] = []
let path = Bundle.main.resourceURL?.appendingPathComponent("songs")
do {
let songs = try FileManager.default.contentsOfDirectory(at: path!, includingPropertiesForKeys: nil, options: FileManager.DirectoryEnumerationOptions.skipsHiddenFiles)
for song in songs {
let strArray = song.absoluteString.components(separatedBy: "/")
var songName = strArray[strArray.count - 1].replacingOccurrences(of: "%20", with: " ")
songName = songName.replacingOccurrences(of: ".wav", with: "")
names.append(songName)
}
} catch {}
return names
}
func playSong(index: Int) {
do {
let songPath = Bundle.main.path(forResource: songs[index], ofType: ".wav", inDirectory: "songs")
try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: songPath!))
audioPlayer.play()
} catch {}
}
override func viewDidLoad() {
super.viewDidLoad()
beatTableView.delegate = self
beatTableView.dataSource = self
self.view.backgroundColor = SettingsService.sharedService.backgroundColor
coverImage.layer.cornerRadius = 20
coverImage.layer.shadowRadius = 7
coverImage.layer.shadowOpacity = 0.8
coverImage.layer.shadowOffset = CGSize(width: 3, height: 3)
coverImage.layer.shadowColor = UIColor.black.cgColor
coverImage.clipsToBounds = true
}
}
extension BeatPackViewController: UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = beatTableView.dequeueReusableCell(withIdentifier: "firstLoopCell", for: indexPath as IndexPath) as! CustomLoopsCell
cell.loopNameLabel.text = data[indexPath.row].loop_name
cell.delegate = self
cell.playButtonOutlet.addTarget(self, action: #selector(CustomLoopsCell.playButtonPressed(_:)), for: .touchUpInside)
// cell.instrumentLabel.text = data[indexPath.row].loops[indexPath.row].Instrument
// cell.producerLabel.text = data[indexPath.row].loops[indexPath.row].producer
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
didTapButton()
}
}
extension BeatPackViewController: CustomLoopsDelegate {
func didTapButton() {
}
}
My CustomCell
import UIKit
import AVFoundation
protocol CustomLoopsDelegate: AnyObject {
func didTapButton()
}
class CustomLoopsCell: UITableViewCell {
weak var delegate: CustomLoopsDelegate?
var songs: [String] = []
var audioPlayer = AVAudioPlayer()
@IBOutlet weak var loopNameLabel: UILabel!
@IBOutlet weak var producerLabel: UILabel!
@IBOutlet weak var instrumentLabel: UILabel!
@IBOutlet weak var playButtonOutlet: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func playButtonPressed(_ sender: UIButton) {
delegate?.didTapButton()
}
}