How do I pass data from one table view to another view when clicking on a cell of a table view? [closed]

StackOverflow https://stackoverflow.com/questions/12377470

Question

I am a beginner and facing some problems when doing my project. I have a table view where I get some NSString value by XML Parsing. This is like : cell 1 : basic name - name, address, phone number, date. cell 2 : featured name - name, address, phone number, date. cell 3 : restaurant name - name, address, phone number, date.

here, cell 1,2,3 are cells of my table view and name, address, phone number, date are the values. I get basic name, featured name, restaurant name and all these values by XML parsing. Now my work is, when I will click on basic name, the detail (name, address, phone number, date) with basic name will pass to an other page. There also be a description of every cell which will be shown when clicking. I am able to do clicking on a cell and go to another page, but unable to pass these datas. Here to be noted that I am using Xcode 4.2 and not using story board.

Was it helpful?

Solution

You need to think in terms of models, views and controllers. Your model is the underlying data that your tables present. The tables are the views, and the controllers are responsible for extracting the data from the model and passing it to the table and views.

So instead of thinking in terms of "how do I pass data between table and view?", think in terms of "how do I provide the data for my view controller so that it can pass the data to its view?"

So in this situation, as Alan suggests, you're going to have to pass the objects around.

Let's assume that your first table's model is an NSArray of NSDictionaries, which is a fairly common result of parsinf XML. Each dictionary has key-value pairs for the restaurant information. Your detail view controller needs a property for a dictionary. In the tableView:didSelectRowAtIndexPath: method, you would instantiate the new view controller for the detail view, then set the view controller's data property to the dictionary in the array at the index that corresponds to the row of the cell that's just been selected.

OTHER TIPS

Your detail view controller simply needs to implement a property or a setter method for the relevant model object that you want it to display. Typically you would set this property in the tableView:didSelectRowAtIndexPath: method on your master UITableView's delegate.

Just check one of the concept using the Delegate variable :

1) Passing Parameter From a View Back To another View's UITableView's Cell

which can be used to pass values to multiple classed at a time.

Alternatively, you can do the same thing using the Singleton class too.

And in your case you can use the Array/Dictionary to pass multiple values to another class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top