Closed vikash4cse closed 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.
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 :-)
/**
@return return value void */
[self performBatchUpdates:^{ [self deleteItemsAtIndexPaths:indexPathsOfOpenedNow]; self.expandedSections[nowOpenedSection] = @(NO); }completion:nil];
// inform the current opened section collapsed. if ([self.delegate respondsToSelector:@selector(collectionView:didCollapseItemAtIndexPath:)]) [self.delegate collectionView:self didCollapseItemAtIndexPath:nowOpenedSectionPath]; } }
(void)handleTapGesture:(UITapGestureRecognizer)sender { if (sender.state == UIGestureRecognizerStateEnded) { CGPoint point = [sender locationInView:self]; NSIndexPath tappedCellPath = [self indexPathForItemAtPoint:point];
if (tappedCellPath && (tappedCellPath.item == 0)) {
NSInteger tappedSection = tappedCellPath.section;
BOOL willOpen = ![self.expandedSections[tappedSection] boolValue];
NSMutableArray* indexPaths = [NSMutableArray array];
for (NSInteger i = 1, maxI = [self.myDataSource collectionView:self numberOfItemsInSection:tappedSection]; i < maxI; i++) {
[indexPaths addObject:[NSIndexPath indexPathForItem:i inSection:tappedSection]];
}
[self performBatchUpdates:^{
if (willOpen) {
[self insertItemsAtIndexPaths:indexPaths];
} else {
[self deleteItemsAtIndexPaths:indexPaths];
}
self.expandedSections[tappedSection] = @(willOpen);
} completion:nil];
if (willOpen) {
[self collapseCurrentOpenedSection: tappedSection];
NSIndexPath* lastItemIndexPath = [NSIndexPath indexPathForItem:[self numberOfItemsInSection:tappedCellPath.section] - 1 inSection:tappedCellPath.section];
UICollectionViewCell* firstItem = [self cellForItemAtIndexPath:tappedCellPath];
UICollectionViewCell* lastItem = [self cellForItemAtIndexPath:lastItemIndexPath];
CGFloat firstItemTop = firstItem.frame.origin.y;
CGFloat lastItemBottom = lastItem.frame.origin.y + lastItem.frame.size.height;
CGFloat height = self.bounds.size.height;
if (lastItemBottom - self.contentOffset.y > height) {
if (lastItemBottom - firstItemTop > height) {
// using setContentOffset:animated: here because scrollToItemAtIndexPath:atScrollPosition:animated: is broken on iOS 6
[self setContentOffset:CGPointMake(0., firstItemTop) animated:YES];
} else {
[self setContentOffset:CGPointMake(0., lastItemBottom - height) animated:YES];
}
}
if ([self.delegate respondsToSelector:@selector(collectionView:didExpandItemAtIndexPath:)]) {
[self.delegate collectionView:self didExpandItemAtIndexPath:tappedCellPath];
}
} else {
if ([self.delegate respondsToSelector:@selector(collectionView:didCollapseItemAtIndexPath:)]) {
[self.delegate collectionView:self didCollapseItemAtIndexPath:tappedCellPath];
}
}
}
[self scrollToItemAtIndexPath:tappedCellPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
} }
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.
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