AsyncDisplayKit icon indicating copy to clipboard operation
AsyncDisplayKit copied to clipboard

CustomCollectionView Swift3 Working Example with MosaicCollectionViewLayout

Open rajeevwhiz opened this issue 10 years ago • 4 comments

I am trying to port CustomCollectionView example to Swift3 but running into issues. Here is the quick log. I have also attached the latest project here: CustomCollectionView.zip

2016-11-04 13:50:03.738 CustomCollectionView[34977:4403329] *** Terminating app due to uncaught 
exception 'NSInternalInconsistencyException', reason: 'layout attributes for supplementary item at 
index path (<NSIndexPath: 0x61800003b120> {length = 2, path = 1 - 0}) changed from <UICollectionViewLayoutAttributes: 0x6080001e5400> 
index path: (<NSIndexPath: 0x60800003c4a0> {length = 2, path = 1 - 0}); element kind: (UICollectionElementKindSectionHeader); 
frame = (10 64; 394 44);  to <UICollectionViewLayoutAttributes: 0x6180001e5300> 
index path: (<NSIndexPath: 0x61800003b120> {length = 2, path = 1 - 0});  element kind: (UICollectionElementKindSectionHeader); 
frame = (10 658.037; 394 44);  without invalidating the layout'
*** First throw call stack:
(
	0   CoreFoundation                      0x00000001082c434b __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x0000000107d2521e objc_exception_throw + 48
	2   CoreFoundation                      0x00000001082c8442 +[NSException raise:format:arguments:] + 98
	3   Foundation                          0x00000001078bbedd -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
	4   UIKit                               0x000000010979f08b __45-[UICollectionViewData validateLayoutInRect:]_block_invoke + 1436
	5   UIKit                               0x000000010979e4e3 -[UICollectionViewData validateLayoutInRect:] + 2984
	6   UIKit                               0x0000000109753605 -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:] + 13233
	7   UIKit                               0x0000000109750079 -[UICollectionView _updateRowsAtIndexPaths:updateAction:] + 377
	8   AsyncDisplayKit                     0x000000010744a4e1 __85-[ASCollectionView rangeController:didInsertNodes:atIndexPaths:withAnimationOptions:]_block_invoke.388 + 65
	9   UIKit                               0x0000000108eb0ada +[UIView(Animation) performWithoutAnimation:] + 90
	10  AsyncDisplayKit                     0x000000010744a27c -[ASCollectionView rangeController:didInsertNodes:atIndexPaths:withAnimationOptions:] + 1324
	11  AsyncDisplayKit                     0x0000000107511776 __85-[ASRangeController dataController:didInsertNodes:atIndexPaths:withAnimationOptions:]_block_invoke + 118
	12  AsyncDisplayKit                     0x00000001074ca814 ASPerformBlockOnMainThread + 100
	13  AsyncDisplayKit                     0x0000000107511604 -[ASRangeController dataController:didInsertNodes:atIndexPaths:withAnimationOptions:] + 772
	14  AsyncDisplayKit                     0x0000000107459c2e __67-[ASDataController _insertNodes:atIndexPaths:withAnimationOptions:]_block_invoke + 606
	15  AsyncDisplayKit                     0x0000000107457f47 __63-[ASDataController insertNodes:ofKind:atIndexPaths:completion:]_block_invoke + 135
	16  AsyncDisplayKit                     0x00000001074eaf44 __30-[ASMainSerialQueue runBlocks]_block_invoke + 308
	17  libdispatch.dylib                   0x000000010bf90980 _dispatch_call_block_and_release + 12
	18  libdispatch.dylib                   0x000000010bfba0cd _dispatch_client_callout + 8
	19  libdispatch.dylib                   0x000000010bf9a8d6 _dispatch_main_queue_callback_4CF + 406
	20  CoreFoundation                      0x00000001082884f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
	21  CoreFoundation                      0x000000010824df8d __CFRunLoopRun + 2205
	22  CoreFoundation                      0x000000010824d494 CFRunLoopRunSpecific + 420
	23  GraphicsServices                    0x000000010e6d4a6f GSEventRunModal + 161
	24  UIKit                               0x0000000108df7f34 UIApplicationMain + 159
	25  CustomCollectionView                0x000000010732a5bf main + 111
	26  libdyld.dylib                       0x000000010c00668d start + 1
	27  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

rajeevwhiz avatar Nov 04 '16 21:11 rajeevwhiz

Somehow layoutAttributesForItem and shouldInvalidateLayout methods are not being called at all. I have no clue why. This explains the exception layout attributes for supplementary item at .. changes from **x** to **y** without invalidating the layout above . I have reviewed the code many times now and not sure what else to try.

 override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
    {
        if (indexPath.section >= _itemAttributes.count) {
            return nil;
        } else if (indexPath.item >= _itemAttributes[indexPath.section].count) {
            return nil;
        }
        return _itemAttributes[indexPath.section][indexPath.item]
    }

override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        if (!(self.collectionView?.bounds.size.equalTo(newBounds.size))!) {
            return true;
        }
        return false;
    }

Only area I can see the issue may be is in porting _delegate:

- (id<MosaicCollectionViewLayoutDelegate>)_delegate
{
  return (id<MosaicCollectionViewLayoutDelegate>)self.collectionView.delegate;
}

which I ported to:

 public var delegate : MosaicCollectionViewLayoutDelegate? {
        get{
            return self.collectionView!.delegate as? MosaicCollectionViewLayoutDelegate
        }
 }

However, since ASCollectionView use asyncDelegate and not delegate, I am getting nil on self.delegate in _itemSizeAtIndexPath when calculating originalSize . Objective-C code should be doing the same but I am not sure why it's not throwing exception.

As a workaround, I defined delegate attribute in MosaicCollectionViewLayout and setting it explicitly inside ViewController, but not sure if that's causing the issue. All other functions seem to be working fine.

rajeevwhiz avatar Nov 04 '16 22:11 rajeevwhiz

Some success! It's working for most part except scrolling. I have fixed all other issues. I would really appreciate if anyone can give me pointers on why scrolling may not be working. Latest Project is attached. Thank you CustomCollectionView.zip

screen shot 2016-11-04 at 7 14 54 pm

rajeevwhiz avatar Nov 05 '16 02:11 rajeevwhiz

I have tried enabling isScrollEnabled on ASCollectionView and scrollDirection on layout... but still no luck.

rajeevwhiz avatar Nov 05 '16 06:11 rajeevwhiz

Finally got it all working. Attached is the working CustomCollectionView Example in Swift3. CustomCollectionView.zip

rajeevwhiz avatar Nov 05 '16 17:11 rajeevwhiz