我正在尝试使用 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/