简体   繁体   中英

AVAudioPlayer table view and when press in table cell play desired sound

I make a table view and when press in table cell will be display detail view with a button for playing sound. How can I make a button play different sounds dependent on table view. Thanks!

you can use this function to check which row is selected in the tableView and then play the sound depending in which cell the user taps.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

//use this to check for the row the user taps. [0 is the first row.]
//If you didn't use indexPath.row -> the function here will be for all the rows.
if indexPath.row == 0 {

     }


}

if you want to go into another view you can use the same method to perform segue.

the Segue Identifier can be change from the Main.storyboard

performSegue(withIdentifier: "soundDetails", sender: self)

Also if you want to pass data into the next view to play the sound you can use this function

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let vc = segue.destination as! SoundDetailsViewController
    //vc.soundId is to access the data in the viewController and pass whatever value you want.
    vc.soundId = mySoundID 


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM