I actually wanted to just add a comment, but reputation issues...
Anyway, my personal favorite way of expanding/collapsing UITableView sections is described in this post: https://stackoverflow.com/a/1941766/2440562
If I am understanding the issue correctly, the menu_headers
and menu_subheaders
would always be visible and only the items would be shown/hidden.
So here it is my idea (let's see if I can explain it well enough):
You probably have an idea how many menu_subheaders
you would have for each menu_header
(static count or the number of elements of an array), so you can add one section for each menu_header (which would actually contain only one row or header) and in-between those you can add the expandable sections (menu_subheaders
), which can be managed as shown in the answer I mentioned above. And as you want to collapse the previously expanded menu_subheader
when tapping on another, you could just reset its boolean value and reload both with the reloadSections
method. You would have to do some calculating for the placement of the menu_headers
and menu_subheaders
, but basically you wouldn't have to deal with cell heights and row insertions and deletions (that actually is my favorite part).
Here it is a quick code snippet of the calculations I've mentioned (not tested, totally improvised):
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return <number_of_menu_headers> + <number_of_menu_subheaders>;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
// handle first menu_header
} else if (indexPath.section < 1 + <number_of_menu_subheaders1>) {
if (indexPath.row == 0) {
// handle the menu_subheader header row
} else {
// handle the rest of the items
}
} else if (indexPath.section == 1 + <number_of_menu_subheaders1>) {
// handle second menu_header
} else if (indexPath.section < 2 + <number_of_menu_subheaders1> + <number_of_menu_subheaders2>) {
if (indexPath.row == 0) {
// handle the menu_subheader header row for the current menu_subheader
} else {
// handle the rest of the items for the current menu_subheader
}
} etc...
}
Again, just an idea...