apploft / APLExpandableCollectionView

UICollectionView subclass with vertically expandable and collapsible sections
MIT License
110 stars 14 forks source link

Collapse CollectionView #2

Closed vikash4cse closed 9 years ago

vikash4cse commented 9 years ago

How can we collapse previous selection when a new item a selected. I want only one item to be in expanded mode at a time

michaelkamphausen commented 9 years ago

You need to subclass APLExpandableCollectionView and override handleTapGesture. The code would be basically the same with a few additions.

If willOpen is true, you would iterate over the self.expandedSections array and find the section indexes where the boolValue is true. Now that you know the section indexes of all expanded sections, you collapse them by creating an array with all the indexPaths in these sections like in lines 73-76, delete the items (line 81) and set the value in self.expandedSections to false (line 83). Finally, you call the delegate method like in lines 106 - 108. As you need to duplicate some code, you might want to refactor your code by creating some helper methods for creating the indexPaths array and for collapsing a section.

googoodalls commented 9 years ago

Hello, vikash4cse,
I just finished this collapse function today, as Michael -- the great author said :-)

just add a collapseCurrentOpenedSection() in the APLExpandableCollectionView.m and add the scrollToItemAtIndexPath to make the new tapped section to be shown on the Top.( i am use >= iOS 7) [self scrollToItemAtIndexPath:tappedCellPath atScrollPosition:UICollectionViewScrollPositionTop animated:YES];

just replace the following code snippet in the corresponding part of APLExpandableCollectionView.m you can get the function that you want.

to Michael, I hope you can add this BOOL flag into the new version as the new feature, I think this is the most common Expandable use case: expand the current tapped section to save more space for user scrolling back and forth.

Thanks for your great product. It is more useful to me :-)

/**

michaelkamphausen commented 9 years ago

Thank you for your feedback and contribution! Today, I added the property you asked for. When you set allowsMultipleExpandedSections = NO, eventually expanded sections collapse while the section tapped by the user expands.