gpt4 book ai didi

objective-c - 使用 UITableView 向下钻取分层数据

转载 作者:太空狗 更新时间:2023-10-30 03:29:11 25 4
gpt4 key购买 nike

我想构建一个应用程序来导航数据层次结构。此层次结构类似于我在下图中显示的结构。结构的一个分支可以比其他分支有更多的层次。

enter image description here

我想像这样使用 TableView 深入了解此结构:

    A > AA > AAB > AABA

    A > AB > ABA

我想知道的是,我是否可以在故​​事板中只有一个 TableViewController 并重用它来显示每一层新数据?我不想在 Storyboard中将一堆 tableviewcontrollers 链接在一起,因为我不确定我是否会有 3、4 或 100 级的层次结构。

最佳答案

要在 Storyboard中仅绘制一个 VC 来递归到任意深度,请使用 Storyboard绘制顶层:一个导航​​ Controller ,其根部有一个 TableView Controller 。表 vc 应该是一个 UITableView 子类,并且在 Storyboard 中有一个 Storyboard 标识符(例如,“MyCustomTableVC”)。

为 MyCustomTableVC 提供一个公共(public)属性(例如,ModelItem *node),指示它应该呈现模型中的哪个节点。

当表 vc 获取 didSelectRowAtIndexPath 时,让它创建自己的另一个实例,而不是 Storyboard segues...

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle: nil];
// Imagine it's called MyCustomTableVC
MyCustomTableVC *newVC = (MyCustomTableVC*)[mainStoryboard
instantiateViewControllerWithIdentifier: @"MyCustomTableVC"];

将 newVC 模型节点设置为所选项目(模型中位于 indexPath.row 的项目),然后推送到它...

// making up a lot of stuff about your model here, but hopefully you get the idea...
newVC.node = [self.model objectAtIndex:indexPath.row];
[self.navigationController pushViewController:newVC animated:YES];

这种方法的好处是可以获得所有导航 Controller 行为:动画按下和弹出、后退按钮等。

关于objective-c - 使用 UITableView 向下钻取分层数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15115637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com