0

in my app i send a GET to my server and receive some response. I have TavleView and TableViewController classes expect from my main view controller class. I do the parsing in my main ViewController and i want to populate the TableViewCells with the results i receive from the first ViewController. And also i want to display the details of any cell in a new ViewController called DetailViewController. Now any navigation amongst the Views work as how i want..

ilhan çetin
  • 383
  • 5
  • 19
  • I couldn't understand your question, do you mean you are on a view and you want to send data to another view (where the other is a table)? – antf Jan 23 '12 at 16:42
  • I m not sure if exactly we mean the same things but you have come close.. I send the GET in the first ViewController and I need to populate the Tableview with the response of that GER. But i'm asked to show the TableView in a second view. Here is my code, http://stackoverflow.com/questions/8969135/how-to-populate-uitableview-with-the-responce-of-json-from-a-different-viewcontr Since i'm a newbie i shared almost all of the codes(i looked at some tutorials but my code never worked) – ilhan çetin Jan 23 '12 at 16:55

3 Answers3

2

Well @ilhan çetin based on your request in the comment above here is a deeper explanation of the solution I propose.

First, in case you don't know what is a Shard Data Model, a Shared Data Model is a class that we define in our project and we create a static instance of it. Since that instance is static this means it exists for all our classes all the time. We use this static instance to share information between different classes in our project like what you need here in your question. For example if you feel that you need to send a string value from a class to a class you can put in the Shared Instance an NSString member. Here is how we do the Shared Instance:

The .h file (assuming that my class is named MyDataModel):

#import <Foundation/Foundation.h>

@interface MyDataModel : NSObject
{
    NSString *stringToBeShared;
}

@property (nonatomic, retain) NSString *stringToBeShared;

+ (MyDataModel *) sharedInstance;

@end

In the .m file:

#import "MyDataModel.h"

@implementation MyDataModel

@synthesize stringToBeShared;

static MyDataModel *_sharedInstance;

+ (MyDataModel *) sharedInstance
{
    if(!_sharedInstance)
    {
        _sharedInstance = [[MyDataModel alloc] init];
    }
    return _sharedInstance;
}

@end

From now on in our program, in any class that we need to access this shared string we will #import "MyDataModel.h" and access the string by: [MyDataModel sharedInstance].stringToBeShared. Now that we have a sharing mechanism ready we will move on to see how to populate the table in the other view.

In your code there is a comment that read:

//i want to populate TableViewCells with the messID and the details of each cell(push in new view controller) will contain 'content' above

You are asking for 2 things in this line but only one can be done in this part of the code. We will now populate the table and in the view of the table when you click on a row we will push the details you want.

To be able to populate the table we will need a function that will do so in the same class where the table exists. Let us name it populateTable. Well how will we be able to call this function when we are in a different class, and who will we send it the messID value? To send the messID we will use the Shared Instance, so you will add what is needed to hold the messID value. Now to call populateTable we will use Notification Center (described in the previous answer I posted).

A Notification center is a mechanism used to call functions in different classes. So in the class that contains populateTable you will add a listener to it in the viewDidLoad as follows:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(populateTable) name:@"DoPopulateTable" object:nil];

We will use the string @"DoPopulateTable" to call this listener. Now inside your loop you will do 2 things:

  1. Store the messID in your Shared Instance
  2. Call populateTable using: [[NSNotificationCenter defaultCenter] postNotificationName:@"DoPopulateTable" object:nil userInfo:nil];

By this you have populated your table as you asked. Are we missing something? Yes the complete data that you will push later on. If the data is important and will be needed after you turn the device off then you have to store it in a sqlite3 database. If not (I will demonstrate the not!) you can store the information inside a NSMutableDictionary where the keys of this dictionary are the messID (to be able to retrieve them when you click on the table), and the values of the table are NSArrays where each array conatins all the info you need.

Now when you want to push the information in the view that has the table, you can call each element inside the array using [yourArrayName objectAtIndex:theIndexYouWant].

I hope this helps you, and remember, no headaches at all ;)

antf
  • 3,162
  • 2
  • 26
  • 33
  • Thank you very very much. I don't have my mac with me but you have given a great (and also very polite) answer:) Thanks again and again. As i understand this will solve my matter. I wil write you back about the result. – ilhan çetin Jan 23 '12 at 22:52
1

go check out WWDC 2011's podcasts, WWDC 2011 - Session 309 - Introducing Interface Builder Storyboarding.

i believe this is the correct video and it will demostrate you exactly what you are looking for. they revamped UITableView's and added a lot of functionality into the IB storyboard in ios5. it will help demonstrate how you can take data in one View Controller and and display it in a second UIViewtable Controller.

Padin215
  • 7,444
  • 13
  • 65
  • 103
1

I will read the code that provided in your comment above, but in the meanwhile I think this link can help you a lot. It is a way to send information between views, so you can parse your data in one view and send them to another one easily using this technique.

Community
  • 1
  • 1
antf
  • 3,162
  • 2
  • 26
  • 33
  • 1
    I have read your code (fast) and I noticed that the method I provided in the link above will solve your problem. If you want more illustrations please reply. – antf Jan 23 '12 at 17:13
  • I had a fast look at that link but i coulnd't realize how it can be helpful for me. Maybe i'm a little tired so i can't see:) thanks man, if it won't disturb you it would be perfect helping with codes, but i don't want to cause headaches:) – ilhan çetin Jan 23 '12 at 17:23
  • No headaches at all, I will post a better explanation to suit your case using part of your code, but this will take some typing time so please be patient. – antf Jan 23 '12 at 17:29