ios - Swift 中的 ASCollectionView 和 ASCellNode

标签 ios xcode swift asyncdisplaykit

我正在尝试使用 AsyncDisplayKit 将图像加载到我的应用程序中。我正在将我的 Collection View 创建为 ASCollectionView,就像这样,在 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)

这是我的 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/

相关文章:

ios - 可以用Alamofire播放同步下载的视频文件吗?

iphone - ViewController 子类应该在每个子类中都有 [super viewDidUnload] 吗?

ios - 将 NSDictionary 从 json 结果保存到 NSUserDefaults 返回 "attempt to insert non-property list object"错误

ios - 生成的ipa没有安装

Xcode 仪器 : profiled hotspots don't match up with source code

ios - 当我点击 swift 3 中的提交按钮时如何重复文本字段

objective-c - IOS:调用另一个类中的方法

xcode - 游戏延迟: Node count on Xcode simulator vs Node count on my iPhone 5S?

ios - Xcode 7.0.1 未知类

ios - 如何仅为 UIImage 的非白色部分设置色调颜色?