返回默认的 UICollectionViewCell 缺少reuseIdentifier

Jon*_*nas 9 ios swift

如果我尝试返回默认值,UICollectionViewCell应用程序会崩溃,因为它缺少reuseIdentifier:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“从 -collectionView:cellForItemAtIndexPath: 返回的单元格没有重用标识符 - 必须通过调用 -dequeueReusableCellWithReuseIdentifier:forIndexPath: 来检索单元格”

代码:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

     return UICollectionViewCell()
}
Run Code Online (Sandbox Code Playgroud)

UITableViewCells工作得很好。违约退货的最佳实践是什么UICollectionViews

Jon*_*nas 8

对我有用的是:

collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "default")
Run Code Online (Sandbox Code Playgroud)

进而:

return collectionView.dequeueReusableCell(withReuseIdentifier: "default", for: indexPath)
Run Code Online (Sandbox Code Playgroud)

作为默认返回。但我希望有更干净的东西。