很簡單的問題。準備從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;
}
如果你的JSON只包含兩個對象,你可以給每一個到一個數組。你有什麼嘗試將json對象存儲到數組中? –
我知道我可以將它們分配給一個數組,問題是我不這樣做?請你能指點我正確的方向嗎? –
你可以試試這個:[array1 addObject:yourJSon [@「amount」]] –