2014-09-24 37 views
-3

我对xcode/objective-c颇为陌生,现在我面临一个问题。Objective-C选择数组的一部分

在我的应用程序中,我有一个从我的服务器接收Json数组,我通过segue将数组发送到secondViewController。这一切都正确。

在我secondViewController我收到数组,并选择我需要的部分;

self.excersiseSection = [self.schemeData objectForKey:@"user_excersise_sections"]; 

这将输出我推送到该控制器的数组部分,所以没关系。现在,我想 在这个数组中选择嵌套的'数组'(打印我的表格单元格)我试图像上面的代码一样使用索引'exercise'来完成它,但没有成功。

{ 
    "user_training_days": [ 
     { 
      "day_id": 1, 
      "day_name": "Monday", 
      "training": "trainingsdag", 
      "user_excersise_sections": [ 
       { 
        "section_title": "Legs", 
        "exercises": [ 
         { 
          "exercise_title": "Leg press", 
          "excercise_sets": "4", 
          "excercise_reps": "12", 
         } 
        ] 
       },...etc 
+0

'user_excersise_sections'包含一个字典数组,因此您可能有多个'exercise'数组。你想怎么处理? – Droppy 2014-09-24 13:47:04

+0

真的,如果需要的话,我可以重建jSon数组,如果它使事情更简单。我试图达到的是获得一个数组中的练习,所以我可以将它们打印在TableViewCell中 – directory 2014-09-24 13:52:05

+0

@Freshtea查看我的编辑 – l0gg3r 2014-09-25 13:56:35

回答

1

这里是一个示例代码,会遍历你的JSON和打印简单吧

for (NSDictionary *section in excersiseSection) { 
     NSLog(@"--- section_title: %@", section[@"section_title"]); 
     for (NSDictionary *excercise in section[@"exercises"]) { 
      NSLog(@"----- exercise_title: %@", excercise[@"exercise_title"]); 
      NSLog(@"----- excercise_sets: %@", excercise[@"excercise_sets"]); 
      NSLog(@"----- excercise_reps: %@", excercise[@"excercise_reps"]); 
     } 
} 

这会帮助你,来构建你的tableview。

编辑:
这里是一个完整的解决方案,以使您的tableView。
1)在strotybaord中创建一个TableViewController,并在storyboard中分配一个Custom类。
2)在类中粘贴代码
3)您准备好了! :)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return self.excersiseSection.count; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.excersiseSection[section][@"exercises"] count]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return self.excersiseSection[section][@"section_title"]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"]; 
    } 
    NSDictionary *excercise = self.excersiseSection[indexPath.section][@"exercises"][indexPath.row]; 
    cell.textLabel.text = excercise[@"title"]; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Sets: %@, Reps: %@", 
           excercise[@"excercise_sets"], excercise[@"excercise_reps"]]; 
    return cell; 
} 
+0

非常感谢!我检查了代码(它会生成良好的日志),并试图在我的UITableViewCell函数中实现它,但我想我需要一些更多的帮助来弄清楚如何将这些代码实际打印到单元格中。不是我想要的初学者最容易的数组:) – directory 2014-09-24 19:44:42

+0

@Freshtea如何将值放入表视图取决于表视图是基于单元还是基于视图。默认情况下,使用Xcode 6创建的新表格视图是基于视图的,以前它们是基于单元格的。这是什么? – SevenBits 2014-09-25 13:06:52

0

我不知道你的数据模型所采用的结构,但不能尝试类似如下:

self.excersiseSection = [[[self.schemeData objectForKey:@"user_excersise_sections"] objectAtIndex:0] objectForKey:@"exercises"]; 

刚刚链中的通话在一起。请注意,如果user_excersise_sectionsexercises不是NSArray对象,则这将不起作用。

0

我建议建立像UserTrainingDayUserExcersiseSectionExcercise特定的类和使用例如Mantle框架将JSON和模型类之间建立映射。