-2

I am working on a tv tracking app where you can search for tv shows and save them to see when a new episode comes out.

My question is, how would I get the movie data from the cell in the searchTableViewController and present the selected data into the cell on myShowsTableViewController?

Kaushik Makwana
  • 1,329
  • 2
  • 14
  • 24
  • 1
    If you need to share data between controllers, the best way is to have an object act as your data model. When the information is entered, it is sent to the data object; when it needs to be displayed, the controller asks the data object to provide it. – Phillip Mills Dec 11 '18 at 02:02
  • Cells are views. They do not hold data. – matt Dec 11 '18 at 02:41

1 Answers1

1

This is a very broad question and is generally not suitable for StackOverflow, which is for more "granular questions".

That said, I would recommend creating a class called DataManager or something like that. In terms of making your API calls, I would recommend using something like Alamofire for making API calls. Lastly, I would recommend reading up on the Codable protocol for decoding the JSON the API sends back. When you decode your JSON, you'll want to store the structs somewhere where your view controller can see it.

To use CocoaPods, here's a good primer (You'd use them for Alamofire).

Your data manager would look something like this (I'll use NSObject for simplicity's sake, but you could just create your own class):

struct Shows: Codable {
    // Whatever the API sends back, you've got to map it here
}

class DataManager: NSObject {
    var shows: [Shows] = [] // empty array of shows to start

    func methodToGetAPIResponse() {
        // Alamofire call & JSON decoding here
        // Once you get a valid response, shows = try JSONDecoder().decode...
    }
}

You could so something like this for your DataManager in your view controller class:

let dataManager = DataManager()

Then, in your UITableViewDataSource methods, numberOfRowsAtIndexPath would be return dataManager.shows.count and cellForRowAtIndexPath you'd get a hold of the object to populate your labels like so:

let show = dataManager.shows[indexPath.row]

Anyway, that ought to be enough to get you started. Good luck! Try to be more granular with your questions in the future.

Adrian
  • 16,233
  • 18
  • 112
  • 180
  • For what it’s worth, I found this answer to be very insightful (as someone who has recently started learning Swift, but knows other languages). – Nate Dec 11 '18 at 03:45
  • @Nate Thank you. I tried to throw the OP a few bones re: where to start, as I've walked in his shoes. – Adrian Dec 11 '18 at 20:52