Can I create UITableView with sub-cells?
Hi I want to create a table view with sub cells. What I mean by "sub cells" is, when I click a arrow pointer in a cell, it should show its sub cells with cell indentation.
- Is this kind of cells available in iPhone?
- If not, Is this po开发者_开发技巧ssible to do it in some other way?
This isn't a standard capability of UITableView, but actually isn't that hard to implement. What you need to do is:
- Create a custom UITableViewCell (perhaps called 'OutlineTableViewCell'). It should have a disclosure indicator
- Create a class which implements the UITableViewDataSource protocol to manage the tree structure of the rows. It should manage the indent level and disclosure state.
- Implement methods to expand/collapse sections of the tree (triggered by tapping the disclosure indicator)
- Finally, subclass both of these to add the storage and display of your own data in the view
Not sure if that'll work in your case, but in one of my apps, I used the following work around to create a two-level table view with normal (level 1) items toggling (level 2) "sub"-cells:
- I have a standard table view, and use its sections as level 1 items.
- For each section, I use a custom view with a UIButton to act as the toggle.
- I keep track of which sections are toggled on / off through an array, and simply return 0 elements for the section when it's off.
- Sub-cells are just regular cells.
精彩评论