I have a UITableView and have seen this effect and would like to implement it for our the followind data:
menu_header
menu_subheader
* item
* item
menu_subheader
* item
* item
* item
Basically, I would like to show just the header and subeaders and then when the user clicks one of the subheaders, it displays the items (preferably in an animation block) AND adjusts the other cells down or up appropriately. Like this:
Is there a prebuilt component that does this? Thinking about it, it seems like I would like to set these item cells to be hidden. I have seen this https://github.com/peterpaulis/StaticDataTableViewController but it looks like it doesn't work with dynamic data. It seems like this should be really simple. Any ideas on how to get this done? Ideally, I'd like it to be able to when you click it insert the data and then if you click another sub-header, close the other one and add to that sub-header.
moveRow(at:to:) Moves the row at a specified location to a destination location.
To implement "folding" in a table view you have two options:
insert
or deleteRowsAtIndexPaths:withRowAnimation:
methods on the tableView. beginUpdates
followed immediately by endUpdates
to re compute the heights and animate to the new layout. I've created a simple implementation of the second option in this GitHub repo. Please let me know if you have other questions about it.
You have to remove and insert cells into your table view instead of hacking their height.
We use a controller for table views that lets you "hide/show" cells while actually handling the removal/reinsertion of rows in the table view.
The way "Toggle Details" works on the Demo is pretty similar to what you are trying to achieve:
- (IBAction)toggleDetails:(id)sender
{
// Hide if all hiddeable rows are hidden, show all otherwise
[self.topSection setObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)]
hidden:(![self.topSection isObjectAtIndexHidden:1] &&
![self.topSection isObjectAtIndexHidden:2] &&
![self.topSection isObjectAtIndexHidden:3])];
}
Changing the height of cells to "hide" them is not optimal as the delegate will be asked for their sizes many times and cells will still be instantiated and configured yet not visible.
The sample library keeps the row data on memory (one object per row) while reusing cells. This should be ok for most projects, but maybe in your case not only all objects shouldn't be on memory but also you shouldn't fetch all of them at once.
Make your header will be Tableview Section and sub header will be Row... And in didSelectRow delegate method insert rows, that will be your items.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With