diff --git a/DraggableCollectionView/Helpers/LSCollectionViewLayoutHelper.m b/DraggableCollectionView/Helpers/LSCollectionViewLayoutHelper.m index 180b499..5a78d8a 100644 --- a/DraggableCollectionView/Helpers/LSCollectionViewLayoutHelper.m +++ b/DraggableCollectionView/Helpers/LSCollectionViewLayoutHelper.m @@ -40,10 +40,18 @@ - (NSArray *)modifiedLayoutAttributesForElements:(NSArray *)elements if ([layoutAttributes.indexPath isEqual:hideIndexPath]) { layoutAttributes.hidden = YES; } + else { + layoutAttributes.hidden = NO; + } } + + [self rearrangeIndexPathOfLayoutAttributesForElements:elements]; + return elements; } + [self rearrangeIndexPathOfLayoutAttributesForElements:elements]; + if (fromIndexPath.section != toIndexPath.section) { indexPathToRemove = [NSIndexPath indexPathForItem:[collectionView numberOfItemsInSection:fromIndexPath.section] - 1 inSection:fromIndexPath.section]; @@ -65,6 +73,9 @@ - (NSArray *)modifiedLayoutAttributesForElements:(NSArray *)elements if ([indexPath isEqual:hideIndexPath]) { layoutAttributes.hidden = YES; } + else { + layoutAttributes.hidden = NO; + } if([indexPath isEqual:toIndexPath]) { // Item's new location layoutAttributes.indexPath = fromIndexPath; @@ -94,4 +105,40 @@ - (NSArray *)modifiedLayoutAttributesForElements:(NSArray *)elements return elements; } +- (void)rearrangeIndexPathOfLayoutAttributesForElements:(NSArray *)elements +{ + if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) + { + NSComparator cmptr = ^(NSIndexPath *ele1, NSIndexPath *ele2){ + + if (ele1.section < ele2.section) + return (NSComparisonResult)NSOrderedAscending; + + if (ele1.row < ele2.row) + return (NSComparisonResult)NSOrderedAscending; + + return (NSComparisonResult)NSOrderedDescending; + }; + + NSMutableArray *indexPathArray = [NSMutableArray array]; + + for (UICollectionViewLayoutAttributes *layoutAttributes in elements) { + + if([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader] + || [layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionFooter]) + { + continue; + } + + [indexPathArray addObject:layoutAttributes.indexPath]; + } + + NSArray *sortedArray = [[NSArray arrayWithArray:indexPathArray] sortedArrayUsingComparator:cmptr]; + + for (NSInteger index = 0; index < sortedArray.count; ++index) { + ((UICollectionViewLayoutAttributes*)[elements objectAtIndex:index]).indexPath = (NSIndexPath *)[sortedArray objectAtIndex:index]; + } + } +} + @end