使用UITableView的NavigationController转到子单元格(或者我应该如何解释...)

时间:2012-04-28 17:46:55

标签: ios uitableview

我已经搜索了一个关于如何让UITableViewController进入深度的高级教程......

我的情景是: 主要列表是国家。点击其中一个单元格,为您提供该国家/地区的一些城市,并在新的UITableView中显示。这部分很简单 - 但添加“更深”,我错过了对如何做的理解:( 假设纽约市可以有其他选择,如酒店,餐厅,酒吧。 点击餐馆再次为您提供fx的新选项。食物风格,快餐,墨西哥,印度,当地。 如果你看快速路径,它可能是: 国家 - >都市>餐厅 - > FastFood->麦当劳

现在,最困难的部分是,我想保存麦当劳价值,并将其带回view2(城市)

是否有人知道一个好的教程,无论是书面还是Youtube - 它都会粉碎!

提前致谢:)

1 个答案:

答案 0 :(得分:0)

以下是您如何做到这一点。首先在城市视图控制器上创建并合成一个名为selectedValue

的属性
@property (nonatomic, strong) NSString *selectedValue;

然后,当您在最后一级tableViewController上选择该值时,在UINavigationController的导航堆栈中找到City viewController,在该viewController上设置该属性,然后将导航堆栈弹出到该viewController。

因此,在您的上一级viewController中,您的代码看起来像:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *rowValue = [dataArray objectAtIndex:[indexPath row]]; //  e.g. McDonald's

    // loop through view controller stack to find the city viewcontroller
    // (if the index of the city viewcontroller will always be the same you could just go directly to that index)
    NSArray *viewControllers = [self.navigationController viewControllers];
    for (UIViewController *vc in viewControllers) {

        if ([vc isMemberOfClass:[CityViewController class]]) {
            CityViewController *cityVC = (CityViewController*)vc;
            cityVC.selectedValue = rowValue;
            [self.navigationController popToViewController:cityVC animated:YES];
            break;
        }
    }
}