0

I am trying to use AsyncDisplayKit to load images into my app. I am creating my collection view as an ASCollectionView, like so, in the viewDidLoad:

    let layout = UICollectionViewFlowLayout()
    layout.itemSize = CGSize(width: 90, height: 90)
    let collection = ASCollectionView(frame: view.frame, collectionViewLayout: layout)

    collection.asyncDataSource = self
    collection.asyncDelegate = self
    collection.registerClass(cell1.self, forCellWithReuseIdentifier: "cell1")
    self.view.layer.addSublayer(collection.layer)

Here is my nodeForItemAtIndexPath:

func collectionView(collectionView: ASCollectionView, nodeForItemAtIndexPath indexPath: 
NSIndexPath) -> ASCellNode {

   let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell1", 
   forIndexPath: indexPath) as! cell1

    return cell
}

nodeForItemAtindexPath is the AsyncDisplayKit version of cellForItemAtIndexPath. It must be used with the ASCollectionView.

The problem is that the method "collecitonView.dequeueRreusableCellWithReuseIdentifier" returns a UICollecitonViewCell. My cell is not that, it is an ASCellNode. So I get an error: "cast from UICollectionViewCell to unrelated type ASNodeCell always fails".

I also get an error at this line:

  collection.registerClass(cell1.self, forCellWithReuseIdentifier: "cell1")

The error says "attempt to register a cell class which is not a subclass of UICollectionViewCell."

Is there some other way to work around this? I have searched just about everywhere, but there are very few resources available in Swift (or obj c for that matter) concerning asyncdisplaykit, and I can't find a single example project that uses ASCollectionView. Any help would be appreciated.

jjjjjjjj
  • 4,203
  • 11
  • 53
  • 72

1 Answers1

1

With AsyncDisplayKit it is not necessary to register cell classes with your ASCollectionNode/View

In your nodeForItemAtIndexPath you will need to instantiate a new cell node every time rather than dequeueing a cell.

func collectionView(collectionView: ASCollectionView, nodeForItemAtIndexPath indexPath: NSIndexPath) -> ASCellNode {

    let node = ASCellNode()

    return node
}

The AsyncDisplayKit github project has more examples of how to implement this