Reputation: 219
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!
Upvotes: 0
Views: 111
Reputation: 7893
Add the data MusicSet into an array:
var arrMusicSet:[[String:AnyObject]]! = []
Create a Segue from Table Cell to View Controller.
Set Segue Identifier in IBInspector.
Now when you will press on cell, the segue will get fired, not need of didSelectRowAtIndexPath
.
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]
}
}
}
Define a Variable in View Controller that will take up the forwarded musicSet.
var musicSet: MusicSet!
Now assign values to your labels from this musicSet
.
Upvotes: 1