从NSdictionary准备NSArray以显示在下一个VC中

时间:2016-07-05 20:32:27

标签: ios objective-c json

非常简单的问题。

  • 如何从我的NSDictionary中创建两个充满Json数据的数组?
  • 我如何在我的下一个vc中使用这些并将其设置为我的cell.label.text的值?

以下是我目前的代码如下:

GroupsHomeViewController.h

// Toggle View Button (on button click open next view controller and populate table view with group matching data)
- (IBAction)ShowModels:(id)sender {

    NSString *var1 = self.groupLabel.text.lowercaseString;
    NSString *var3 = [var1 stringByReplacingOccurrencesOfString:@" " withString:@""]; //how to remove spaces personal reference.

    NSString *var2 = self.groupLabel.text.lowercaseString;
    NSString *var4 = [var2 stringByReplacingOccurrencesOfString:@" " withString:@""];

    NSURLSession *session = [NSURLSession sharedSession];

    // NSLog(@"%@", var3); used for testing remove upon completion.

    NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.test.com/%@/%@.json", var3, var4]] completionHandler:^(
                                                                                                                                                                                       NSData *data,
                                                                                                                                                                                       NSURLResponse *response,
                                                                                                                                                                                       NSError *error) {
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        //need to assign @amount to a nsarray somehow and then display in next vc.
        //need to also assign amake to an nsarray and send to next vc also.


        NSLog(@"%@", json);

    }];

    //Run the completed task.
    [dataTask resume];
}

ModelViewController.h

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Set number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Set number of rows.
    return 0; //change to count.
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

    }

    //cell.textLabel.text = @amount //...need to find code for this...
    //cell.detailedLabel.text = @model



    // Set data for cell:

    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

    @end

我只想知道如何从我的字典中创建NSArray我已经知道如何将它们设置为我的cell.label.text。谢谢你的帮助。

JSON格式:

    {
    amount = 2;
    make = V8;
}

1 个答案:

答案 0 :(得分:1)

Lee Sugden您可以轻松地从字典中获取数据,然后将对象添加到数组中。

NSMutableArray *arrJsonAmount = [[NSMutableArray alloc]init];
NSMutableArray *arrJsonMake = [[NSMutableArray alloc]init];
[arrJsonAmount addObject:[json objectForKey:@"amount"]];
[arrJsonMake addObject:[json objectForKey:@"Make"]];

UITableView数据源方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
  return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
     return [arrJsonAmount count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    cell.textLabel.text = arrJsonAmount[indexPath.row];
    cell.detailedLabel.text = arrJsonMake[indexPath.row];
    return cell;
}