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
// vKj
I've used a NSDictionary as datasource, this looks like a lot of code, but it's really simple and works very well! how looks here
I created a enum for the sections
sections property:
A method returning my sections:
And then setup my data soruce:
The next methods, will help you to know when a section is opened, and how to respond to tableview datasource:
Respond the section to datasource:
Tools:
Toggle section visibility
Expanding on this answer written in Objective C, I wrote the following for those writing in Swift
The idea is to use sections within the table and set the number of rows in the section to 1 (collapsed) and 3(expanded) when the first row in that section is tapped
The table decides how many rows to draw based on an array of Boolean values
You'll need to create two rows in storyboard and give them the reuse identifiers 'CollapsingRow' and 'GroupHeading'
You have to make your own custom header row and put that as the first row of each section. Subclassing the
UITableView
or the headers that are already there will be a pain. Based on the way they work now, I am not sure you can easily get actions out of them. You could set up a cell to LOOK like a header, and setup thetableView:didSelectRowAtIndexPath
to manually expand or collapse the section it is in.I'd store an array of booleans corresponding the the "expended" value of each of your sections. Then you could have the
tableView:didSelectRowAtIndexPath
on each of your custom header rows toggle this value and then reload that specific section.Then set
numberOfRowsInSection
to check themybooleans
value and return 1 if the section isn't expanded, or 1+ the number of items in the section if it is expanded.Also, you will need to update
cellForRowAtIndexPath
to return a custom header cell for the first row in any section.I got a nice solution inspired by Apple's Table View Animations and Gestures. I deleted unnecessary parts from Apple's sample and translated it into swift.
I know the answer is quite long, but all the code is necessary. Fortunately, you can just copy&past most of the code and just need to do a bit modification on the step 1 and 3
1.create
SectionHeaderView.swift
andSectionHeaderView.xib
the
SectionHeaderView.xib
(the view with gray background) should look something like this in a tableview(you can customize it according to your needs, of course):note:
a) the
toggleOpen
action should be linked todisclosureButton
b) the
disclosureButton
andtoggleOpen
action are not necessary. You can delete these 2 things if you don't need the button.2.create
SectionInfo.swift
3.in your tableview
Some sample code for animating an expand/collapse action using a table view section header is provided by Apple at 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.