我正在尝试使用AsyncDisplayKit将图像加载到我的应用程序中。我像在viewDidLoad中那样,将收藏视图创建为ASCollectionView:

    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)


这是我的nodeForItemAtIndexPath:

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

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

    return cell
}


nodeForItemAtindexPath是cellForItemAtIndexPath的AsyncDisplayKit版本。它必须与ASCollectionView一起使用。

问题在于方法“ collecitonView.dequeueRreusableCellWithReuseIdentifier”返回UICollecitonViewCell。我的手机不是,它是ASCellNode。所以我得到一个错误:“从UICollectionViewCell到无关类型ASNodeCell的广播始终失败”。

我在这一行也收到错误:

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


该错误显示为“尝试注册不是UICollectionViewCell的子类的单元格类”。

还有其他解决方法吗?我已经搜索了几乎所有地方,但是在Swift(或obj c)中,关于asyncdisplaykit的资源很少,而且我找不到使用ASCollectionView的单个示例项目。任何帮助,将不胜感激。

最佳答案

使用AsyncDisplayKit,不必向ASCollectionNode/View注册单元格类

在您的nodeForItemAtIndexPath中,您每次都需要实例化一个新的单元节点,而不是使单元出队。

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

    let node = ASCellNode()

    return node
}


AsyncDisplayKit github项目提供了更多有关如何实现此目的的示例

关于ios - Swift中的ASCollectionView和ASCellNode,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34606135/

10-15 17:31