我从网络服务 API 得到了一个响应,如下所示:
{
"springs": [{
"name": "springName",
"leafs": [{
"name": "leafName",
"towns": [{
"name": "townName",
},{
"name": "leafName2",
},{
我列出了所有 Leafs
在LeafViewController
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;
}
我的问题是有时没有Town
与 Leaf
相关联,在这种情况下,我需要检查一下,如果是 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/