用UITableView进行层次数据的下拉 [英] Drill down hierarchical data with UITableView
问题描述
我想构建一个应用程序来导航数据层次结构。这个层次结构类似于我在下面的图像中显示的结构。结构的一个分支可能比其他分支更多。
我想使用TableViews深入了解此结构,如下所示:
A> AA> AAB> AABA
或
code> A> AB> ABA
我想知道的是,如果我的故事板中只能有一个TableViewController并重新使用显示每个新的数据级别?
我不想在我的故事板中将一堆 tableviewcontrollers
链接在一起,因为我不知道我是否会拥有3,4或100级。
只需在故事板中绘制一个VC即可递归到任意深度,使用storyboard来绘制顶层:一个带有视图控制器的导航控制器,它是根。表vc应该是一个UITableView子类,并且在故事板(例如MyCustomTableVC)中有一个故事板标识符。
给MyCustomTableVC一个公共属性(例如,ModelItem *节点),表明你的模型中应该存在哪个节点。
而不是storyboard segues,当表vc获取 didSelectRowAtIndexPath
,创建另一个实例...
UIStoryboard * mainStoryboard = [UIStoryboard storyboardWithName:@MainStoryboard
bundle:nil];
//想象一下它叫MyCustomTableVC
MyCustomTableVC * newVC =(MyCustomTableVC *)[mainStoryboard
instantiateViewControllerWithIdentifier:@MyCustomTableVC];将$ new $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ b 将newVC模型节点设置为所选项目(modelPath.row中模型中的项目)然后推送它...
//在这里构建了很多关于你的模型的东西,但希望你得到想法...
newVC.node = [self.model objectAtIndex:indexPath.row];
[self.navigationController pushViewController:newVC animated:YES];
这种方法有利于获取所有导航控制器的行为:动画推动和弹出,后退按钮,等等。
I want to build an app to navigate a data hierarchy. The structure of this hierarchy is something like the one I'm showing in the following image. A branch of the structure could have more levels than the others.
I would like to drill-down this structure with TableViews like this:
A > AA > AAB > AABA
or
A > AB > ABA
What I would like to know is if I can have only one TableViewController in my storyboard and reuse it to display each new level of data?
I don't want to have a bunch of tableviewcontrollers
linked together in my storyboard because I am not sure if I will have a hierarchy of 3, 4, or 100 levels.
解决方案 To recurse to arbitrary depth with only a single VC painted in storyboard, use storyboard to paint the top level: a navigation controller with a table view controller at it's root. The table vc should be a UITableView subclass, and have a storyboard identifier in storyboard (say, "MyCustomTableVC").
Give MyCustomTableVC a public property (say, ModelItem *node) that indicates which node in your model it should present.
Instead of storyboard segues, when the table vc gets didSelectRowAtIndexPath
, have it create another instance of itself...
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle: nil];
// Imagine it's called MyCustomTableVC
MyCustomTableVC *newVC = (MyCustomTableVC*)[mainStoryboard
instantiateViewControllerWithIdentifier: @"MyCustomTableVC"];
Set the newVC model node to the selected item (the item in your model at indexPath.row), and then push to it...
// 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];
This approach has the benefit of getting all the navigation controller behavior: animated pushes and pops, back buttons, etc.
这篇关于用UITableView进行层次数据的下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!