1

[Transfering data from one TableViewCell to another TableViewCell][1]

I have CountriesViewController with tableView where is a list of different countries. When I choose a country I want to get PlacesViewController with tableView which shows different locations in this country. When I choose another country in CountriesViewController I want to get another list of locations related to this country.

Is it possible?

First ViewController

Second View Controller

If somebody had any suggestions and can show some code examples it would be really great!

Thank you guys in-advance!

Pavel Bogart
  • 405
  • 5
  • 16
  • you are talking about tableViewCell or after selecting cell you have new view or new tableview cell, show us something more, code or screenshot – Lu_ Aug 02 '16 at 12:01
  • I think you mean, after tapping a tableViewCell, you want to open another tableViewController which will display other cells. A Cell can't display on its own, a cell must live inside a tableVIewController. There is nothing new or unique here, simply do some research on passing data between viewControllers, only difference is detecting which cell was tapped – Simon McLoughlin Aug 02 '16 at 12:05
  • so this is a whole tableView not tableView cell, but after selecting cell you can use controller for this tableview and implement method prepareForSegue, there you can pass the data from cell. In overal i think you should reconsider your idea, if you select something and you want to display data you may not need new tableview but just a viewController – Lu_ Aug 02 '16 at 12:05
  • 1
    Possible duplicate of [How to pass prepareForSegue: an object](http://stackoverflow.com/questions/7864371/how-to-pass-prepareforsegue-an-object) – Simon McLoughlin Aug 02 '16 at 12:06
  • I doubt whether it should be marked a duplicate, because this question is about tableviewcontrollers, and therefore you don't use prepareForSegue but you can use the function that checks which cell is tapped. @Simon McLoughlin – Eric Aug 02 '16 at 12:26
  • @Lu_ Maybe I misunderstood something. Just want to clarify one more time. I have CountriesViewController with TableView where is a list of different countries. When I choose a country I want to get PlacesViewController with tableView which shows different locations in this country. When a choose another country in CountriesViewController I want to get another list of locations related to this country. As I understand it's not possible and I have to use another option. – Pavel Bogart Aug 02 '16 at 12:28
  • prepareForSegue() should suffice just enough. Since the view controllers are on two different place. Use segue to pass datas. – Ariel Aug 02 '16 at 12:46
  • @Ariel But as I know when I set a segue from a cell to another View Controller I named an identifier. How can I use segue with the same identifier but with different data? – Pavel Bogart Aug 02 '16 at 13:32
  • yes It is possible.. Only you need to transfer the data from one controller to another.. Make custom cell as you want and then pass the valu from transferred array – JAck Aug 02 '16 at 13:33
  • @Eric Could you tell me which function can I use ? – Pavel Bogart Aug 03 '16 at 08:53
  • Use optionals to send the data. In the VC2 add an optional variable called country. Then, you can get the VC2 from VC1 using the segue method prepareForSegue(). Now, get that country optional and set it to the table cell's country name. And, viola. There you go! If you want I can make that an answer with some codes! But, how does VC2 know the list of cities in it? Are you using a predefined city and country list or fetching from the cloud? – Ariel Aug 03 '16 at 12:05
  • @PavelBogart I am typing my answer:) – Eric Aug 03 '16 at 15:58

2 Answers2

0

So I guess I have to implement this chunk of code but it's just for transferring data from from ViewController with tableView to another ViewController without tableView.

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "detailSegue" {
        if let indexPath = tableView.indexPathForSelectedRow {
            let destinationController = segue.destinationViewController as!
PlacesViewController
            destinationController.placesImage =
countryImages1[indexPath.row] 
        }
   }
}
Pavel Bogart
  • 405
  • 5
  • 16
  • Which part tells you that the second one should be without table view? It can have a table view - you should use the passed data (for example a country identifier of sorts), and load the places (so the data for the new cells) based on this. – Losiowaty Aug 02 '16 at 14:07
  • @Losiowaty Please, could you explain me more detailed what I should do? I didn't get it yet! – Pavel Bogart Aug 03 '16 at 08:51
  • @PavelBogart post your codes please, if its not licensed. Then we can help you further on. – Ariel Aug 03 '16 at 12:07
0

First you have to make a array of all the data, then when a specific row is tapped, you send the name of that country to the second tableview and in there just filter the array based on the country. The code beneath just shows you how to check which row is tapped and send that information to the second tableview:

var currentCountry : String!

    class View1 : UITableViewController {
        //tableview stuff

        override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
            let country = tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.text //Here I use the title to define which country is clicked
            currentCountry = country
        }
    }


    class View2 : UITableViewController {
        var country = currentCountry
        //more tableview stuff
    } 
Eric
  • 1,210
  • 8
  • 25