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

标签 objective-c data-structures uitableview uistoryboard

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

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/

相关文章:

iOS TableView 在每个单元格中显示相同的对象

ios - 如何使用后台操作管理出队单元

javascript - 解析[错误] : success/error was not called (Code: 141, 版本 : 1. 9.0)

iphone - 一旦应用程序激活,我如何测试当前 View Controller 是否是第一响应者/最顶层 View Controller

ios - 在真正的 Apple Watch 上调试 WatchKit - 没有任何反应

ios - 如何在 UITableViewCell 上突出显示 UIButton

ios - 多个 UITableViewCell 子类的一个 XIB

c++ - 在C++程序中删除全局变量

java - getLastIndexOf(int item) 链接列表

vector - Clojure - 为一个人定义一个数据结构