Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

showing and hiding a specific cell type in a UITableView (possibly with animation)

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:

enter image description here

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.

like image 500
timpone Avatar asked Sep 04 '14 23:09

timpone


People also ask

Which method is used to allow moving of row in UITableView?

moveRow(at:to:) Moves the row at a specified location to a destination location.


3 Answers

To implement "folding" in a table view you have two options:

  • Control the number of cells in a section based on a folded/unfolded property per section. When folding or unfolding, use the insert or deleteRowsAtIndexPaths:withRowAnimation: methods on the tableView.
  • Control the height of the cells using the delegate method. Return zero for folded sections based on a folded/unfolded property per section. When folding or unfolding, call 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.

like image 113
jrturton Avatar answered Oct 23 '22 23:10

jrturton


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.

like image 2
Rivera Avatar answered Oct 24 '22 00:10

Rivera


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.

like image 1
user716937 Avatar answered Oct 24 '22 01:10

user716937