Implementing UITableView Tree Structure

2020-07-24 04:37发布

问题:

I'm trying to setup a UITableView for displaying nested threaded items. I'm using Core Data and a NSFetchedResultsController, but am not sure what my approach should be. I want to display the information like this:

  • Item #1
    • Item #1.1
    • Item #1.2
    • Show All
  • Item #2
    • Item #2.1
    • Item #2.2
    • Show All

In the above, each sub-section has a 'Show All' button that when clicked will be replaced with additional subitems (each bullet above is a single table view cell). Anyone done anything similar and have any approach hints? Thanks.

回答1:

I wouldn't bother trying to cram a NSFetchedResultsController into a mode it was not created for. The FRC has no intrinsic support for nested tables and I think it would take more work to hack it to do a nested table than it would to just write a custom tableview controller to handle the problem. That is doubly true if your table will display objects from two or more entities.

Moreover, I would caution against using nested tables on a handheld mobile. The iPhone screen will only show about 11 standard sized rows. So, given your example above, you will only see two or three node headings at anyone time. Worse, if you have a node with more than 11 leaves, you will see nothing but a screen of leaves and the hierarchical nature of the tableview will be disguised.

In most circumstances, a hierarchy of tableviews, each showing on a single separate level, is almost always the best design.



回答2:

You could use section headings to split your list, with the heading being the item name. All the items would be available and you can simply scroll through them all at once. As you only want one level of indent this might work well for you.

I have seen quite an effective use of sub-items in an app called GoTasks (free download from the app store) which shows sub-tasks as multiple indents, (though with no collapsing), I think it's pretty effective, even without the screen space on my iPhone. Unfortunately, my iPhone development experience isn't sufficient to tell you how that's done.