2013-05-29 137 views
0

我得到一个回响应从Web服务API像这样:在LeafViewController做什么

{ 
    "springs": [{ 
     "name": "springName", 
     "leafs": [{ 
      "name": "leafName", 
      "towns": [{ 
       "name": "townName", 
      },{ 
      "name": "leafName2", 
      },{ 

我列出了所有Leafs的查看,如果Leaf有与之关联的Towns的列表,则会跳转到TownViewController表格视图以列出关联的Towns

我有所有正确的在我的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"standardCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    Spring *spring = [springs objectAtIndex:indexPath.section]; 
    Leaf *leaf = [sport.leafs objectAtIndex:indexPath.row]; 

    cell.textLabel.text = leaf.shortName; 

    return cell; 
} 

我的问题是,有时候没有与Leaf相关Town,在这种情况下,我需要检查所以如果Leaf细胞按下后,我可以弹回到主视图控制器,而不是转到列出Town的下一个视图控制器。

现在,如果我选择一个Leaf单元格,并且没有Town s与之相关联,则它会继续到空白的TownViewController,因为没有任何城镇关联。

如何检查JSON响应,以便我知道是继续访问下一个TownViewController还是回到MainViewController

我可以发布任何额外的代码,谢谢你的帮助!

回答

2

像这样的东西应该工作

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    Spring *spring = [springs objectAtIndex:indexPath.section]; 
    Leaf *leaf = [sport.leafs objectAtIndex:indexPath.row]; 
    if(leaf.town) 
     //Do whatever 
    else 
     //Do something different 
} 

显然,这取决于你有你的叶对象正确配置了这一点。

+0

感谢您的回应!这很有道理,我今晚会给它一个镜头 - – Realinstomp