Could somebody tell me the way to perform UITableView
expandable/collapsible animations in sections
of UITableView
as below?
or
Could somebody tell me the way to perform UITableView
expandable/collapsible animations in sections
of UITableView
as below?
or
To implement the collapsible table section in iOS, the magic is how to control the number of rows for each section, or we can manage the height of rows for each section.
Also, we need to customize the section header so that we can listen to the tap event from the header area (whether it's a button or the whole header).
How to deal with the header? It's very simple, we extend the UITableViewCell class and make a custom header cell like so:
then use the viewForHeaderInSection to hook up the header cell:
remember we have to return the contentView because this function expects a UIView to be returned.
Now let's deal with the collapsible part, here is the toggle function that toggle the collapsible prop of each section:
depends on how you manage the section data, in this case, I have the section data something like this:
at last, what we need to do is based on the collapsible prop of each section, control the number of rows of that section:
I have a fully working demo on my Github: https://github.com/jeantimex/ios-swift-collapsible-table-section
If you want to implement the collapsible sections in a grouped-style table, I have another demo with source code here: https://github.com/jeantimex/ios-swift-collapsible-table-section-in-grouped-section
Hope that helps.
This is the best way i found to create expandable table view cells
.h file
.m file
Table view delegate methods
I found another relatively simple way to solve that problem. By using this method we will not required to alter our cell which is almost always related to data array index, potentially causing mess in our view controller.
First, we add this following properties to our controller class:
collapsedSections
will save collapsed section numbers.sectionViews
will store our custom section view.Synthesize it:
Initialize it:
After that, we must connect our UITableView so it can be accessed from within our view controller class:
Connect it from XIB to view controller using
ctrl + drag
like usually.Then we create view as custom section header for our table view by implementing this UITableView delegate:
Next, we implement method to save our previously created custom section header in class property:
Add
UIGestureRecognizerDelegate
to our view controller .h file:Then we create method
attachTapGestureToView:
Above method will add tap gesture recognizer to all of section view we created before. Next we should implement
onTap:
selectorAbove method will invoked when user tap any of our table view section. This method search correct section number based on our
sectionViews
array we created before.Also, we implement method to get wihch section of header view belongs to.
Next, we must implement method
toggleCollapseSection:
This method will insert/remove section number to our
collapsedSections
array we created before. When a section number inserted to that array, it means that the section should be collapsed and expanded if otherwise.Next we implement
removeCollapsedSection:
,addCollapsedSection:section
andisCollapsedSection:section
This three method is just helpers to make us easier in accessing
collapsedSections
array.Finally, implement this table view delegate so our custom section views looks nice.
Hope it helps.
I have done the same thing using multiple sections .
I ended up just creating a headerView that contained a button ( i saw Son Nguyen's solution above after the fact, but heres my code.. it looks like a lot but it's pretty simple):
declare a couple bools for you sections
...code
now in your tableview delegate methods...
and finally the function that gets called when you touch one of the section header buttons:
Some sample code for animating an expand/collapse action using a table view section header is provided by Apple here: Table View Animations and Gestures
The key to this approach is to implement
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
and return a custom UIView which includes a button (typically the same size as the header view itself). By subclassing UIView and using that for the header view (as this sample does), you can easily store additional data such as the section number.