2016-07-05 21 views
0

很簡單的問題。準備從NSdictionary NSArray顯示在下一個VC

  • 如何從我的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; 
} 
+0

如果你的JSON只包含兩個對象,你可以給每一個到一個數組。你有什麼嘗試將json對象存儲到數組中? –

+0

我知道我可以將它們分配給一個數組,問題是我不這樣做?請你能指點我正確的方向嗎? –

+0

你可以試試這個:[array1 addObject:yourJSon [@「amount」]] –

回答

1

李薩格登,你可以很容易地從字典中的數據與您的CA對象添加到陣列。

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; 
} 
+0

如果陣列尺寸不同,該怎麼辦? – Droppy

+0

我希望得到的數據幾乎都在那裏。所以根據我的數組大小是平等的。 – user3182143

+0

那太天真了。你永遠不應該信任你收到的數據,特別是如果它會讓你的應用崩潰,這可以。 – Droppy