很简单的问题。
- 如何从充满 Json 数据的 NSDictionary 创建两个数组?
- 然后我如何在我的下一个 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];
}
Model View Controller .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;
}
最佳答案
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;
}
关于ios - 从 NSdictionary 准备 NSArray 以在下一个 VC 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38212225/