我正在制作一款具有在Swift中播放音频功能的应用。所有音频文件都显示在UITableView
中,当我单击该行时,必须播放相应的音频文件。问题是,例如,当我单击第一行时它不播放音频,但如果我单击第二行,则播放第一行的音频。
Image of tableView
代码如下:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
var mp3file: AnyObject = mp3sPaths[indexPath.row]
cell.textLabel?.text = mp3file.lastPathComponent
return cell
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
var mp3file = mp3sPaths[indexPath.row] as NSString
var mp3URL: NSURL = NSURL.fileURLWithPath(mp3file)!
var error: NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: mp3URL, error: &error)
audioPlayer?.stop()
audioPlayer?.prepareToPlay()
audioPlayer?.play()
}
答案 0 :(得分:1)
当您使用DeselectRowAtIndexPath
时,您正在使用SelectRowAtIndexPath
。