facebookarchive / AsyncDisplayKit

Smooth asynchronous user interfaces for iOS apps.
http://asyncdisplaykit.org
Other
13.41k stars 2.2k forks source link

[Examples] MosaicCollectionViewLayout in Swift #2674

Closed jonybur closed 7 years ago

jonybur commented 7 years ago

I'm trying to port the example project CustomCollectionView to Swift, leaving the layout class 'MosaicCollectionViewLayout' as it comes (using a bridging header).

First I initalize the ASCollectionNode as follows:

The ASCollectionNode lives inside an ASViewController class, that also inherits from ASCollectionDelegate and ASColllectionDataSource (trying to make it just like the objc project)

class BSWaterfallView : ASViewController<ASCollectionNode>, ASCollectionDelegate, ASCollectionDataSource{   
    let layoutInspector : MosaicCollectionViewLayoutInspector = MosaicCollectionViewLayoutInspector();
    var collectionNode : ASCollectionNode!;
    var sections : NSMutableArray!;

In init() I run this lines to initialize the variables.

let layout : MosaicCollectionViewLayout = MosaicCollectionViewLayout();
layout.numberOfColumns = 2;
layout.headerHeight = 44.0;
collectionNode = ASCollectionNode(collectionViewLayout: layout);

Right after that declaration and filling the NSMutableArray "sections", I'm trying to run registerSupplementaryNode just like in the sample project, but I get a crash.

The line that breaks the project:

collectionNode.view.registerSupplementaryNode(ofKind: UICollectionElementKindSectionHeader);

The error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MosaicCollectionViewLayoutInspector collectionView:numberOfSectionsForSupplementaryNodeOfKind:]: unrecognized selector sent to instance 0x170015f80'

What am I missing? I know this might be a rather newbie question but I couldn't find any documentation to find a solution to my issue.

Thank you.

hannahmbanana commented 7 years ago

@jonybur - have you looked at the swift version of the CustomCollectionView example project?

jonybur commented 7 years ago

Oh golly, I didn't see that example before. I will check it out and work with it from now on. Thank you so much.

hannahmbanana commented 7 years ago

@jonybur - no worries! It was just merged last night, so it hasn't been around long. :)

nhatlee commented 7 years ago

I got the same crashed, after I removed the line: _collectionNode.registerSupplementaryNode(ofKind: UICollectionElementKindSectionHeader). I removed it because I don't want a title for the section(I just have one section without title). So how to fix it?

jlalvarez18 commented 7 years ago

You shouldn't need to register supplementary nodes since they are not reused like in a standard UICollectionView. Just return the nodes in the appropriate datasource methods and you should be ok.

nhatlee commented 7 years ago

could you explain more detail, maybe some code will more clearer?