Scott Scott - 1 year ago 161
iOS Question

iOS - Showing info from tableView didSelect

I currently have a tableView and when the cell has been pressed I want to show certain data. The tableView currently has 2 cells 'Set One', 'Set Two'.

If cell with title 'Set One' didSelect then I would like the next View Controller to show the data for this cell.

Here is the data from Core Data:

// MusicSet One
defaultMusicSet!.uri = "spotify:track:1puJlKuYGH58SAFgXREUpE"
defaultMusicSet!.duration = 30.0
defaultMusicSet!.starttime = 40.0
defaultMusicSet!.xfade = 10.0
defaultMusicSet!.voiceover = "1.25s Next Up.mp3"

// MusicSet Two
defaultMusicSet!.uri = "spotify:track:59XO64Te17WcmnmMQXQbOj"
defaultMusicSet!.duration = 30.0
defaultMusicSet!.starttime = 34.75
defaultMusicSet!.xfade = 10.0
defaultMusicSet!.voiceover = "1.25s Rest.mp3"

In the ViewController I have textFields called 'txtUri', 'txtDuration' etc. Now if I press on the first cell I would like to show //MusicSet One data only, and the same if I press on the 2nd cell it will only show the //MusicSet Two data.

I know the code will have to go inside the tableView (didSelect), but how to show certain information i'm not 100% sure on how to do this?

This won't work, but I kinda know that I need something like this? or similar...

let musicData: playViewController?

musicData?.txtUri.text = Music.DefaultMusicSet.uri

Any help would be great!

Answer Source
  1. Add the data MusicSet into an array:

    var arrMusicSet:[[String:AnyObject]]! = []
  2. Create a Segue from Table Cell to View Controller.

  3. Set Segue Identifier in IBInspector.

  4. Now when you will press on cell, the segue will get fired, not need of didSelectRowAtIndexPath.

  5. Now get the MusicSet according to selected row inside prepareForSegue method:

    // MARK: - Navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "MySegueIndentifier" {
            if let destVC = segue.destinationViewController as? MyViewController {
                let selected = self.tableview.indexPathForSelectedRow!.row
                destVC.musicSet = arrMusicSet[selected]                
  6. Define a Variable in View Controller that will take up the forwarded musicSet.

    var musicSet: MusicSet!
  7. Now assign values to your labels from this musicSet.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download