How to use parsed elements of the first ViewContro

2019-03-03 17:53发布

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..

3条回答
smile是对你的礼貌
2楼-- · 2019-03-03 18:14

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 ;)

查看更多
女痞
3楼-- · 2019-03-03 18:27

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.

查看更多
一纸荒年 Trace。
4楼-- · 2019-03-03 18:35

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.

查看更多
登录 后发表回答