Slide 95
Slide 95 text
import UIKit
class SongsViewController : UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
let songs = DataStore.songs()
// MARK: UIViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "songDetailsSegue") {
if let indexPath = self.tableView.indexPathForSelectedRow {
let songDetailsViewController = segue.destination as! SongDetailsViewController
let selectedSong = songs[indexPath.row]
songDetailsViewController.selectedSong = selectedSong
}
}
}
// MARK: UITableViewDataSource
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return songs.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "songCellIdentifier") as! SongViewCell
let song = songs[indexPath.row]
cell.bind(song: song)
return cell
}
// MARK: UITableViewDelegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "songDetailsSegue", sender: self)
}
}