ios - 选择没有关联 JSON 响应的单元格时该怎么办

标签 ios json restkit

我从网络服务 API 得到了一个响应,如下所示:

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

我列出了所有 LeafsLeafViewController TableView ,如果 Leaf有一个列表 Towns与它关联,然后它转到 TownViewController TableView 以列出关联的 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;
}

我的问题是有时没有TownLeaf 相关联,在这种情况下,我需要检查一下,如果是 Leaf单元格被按下然后我可以弹出回到主视图 Controller 而不是去下一个列出 Town 的 View Controller

现在,如果我选择 Leaf没有 Town 的单元格s 与之关联,然后它转到 TownViewController这是空白的,因为没有关联的城镇。

我将如何检查 JSON 响应,以便我知道是转到下一个 TownViewController 还是弹出回 MainViewController ?

我可以发布任何必要的额外代码,感谢您的帮助!

最佳答案

像这样的东西应该可以工作

- (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
}

这显然取决于您是否为此正确配置了叶对象。

关于ios - 选择没有关联 JSON 响应的单元格时该怎么办,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16824876/

相关文章:

iphone - Objective-C + RESTKit : Problems with getting Objects

ios - restKit 具有某些不与核心数据管理对象一对一匹配的 JSON 响应

ios - 我怎样才能让 UIButton 粘在 UIView 的底部

ios - 区分何时调用 UIApplicationDidBecomeActiveNotification

ios - 多个 GMSMarker 显示/隐藏耗时

ios - 如何理解 iPhone 的崩溃日志

json - Http 正文中的 Post 字符串值

javascript - 解析和字符串化一个json对象jquery

c# - 如何使用 C# 字典在 Dapper 中映射银行的 JSON?

iphone - RestKit:使用命名路由的 POST 请求