nay*_*yem 6 uitableview ios uicollectionview uicollectionviewcell swift
我正在和一个人一起工作UICollectionView.按照dequeueReusableCell(withReuseIdentifier:for:)预期在调用此方法之前必须使用该register(_:forCellWithReuseIdentifier:)方法注册类或nib文件,我在我的viewDidLoadfunc中添加了一行作为
self.collectionView!.register(PhotoCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
Run Code Online (Sandbox Code Playgroud)
现在,当我使用单元格进行出列和配置时,我收到错误和应用程序崩溃.
致命错误:在展开Optional值时意外发现nil
这是我的DataSource方法:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier,
for: indexPath) as! PhotoCollectionViewCell
let aPhoto = photoForIndexPath(indexPath: indexPath)
cell.backgroundColor = UIColor.white
cell.imageView.image = aPhoto.thumbnail //this is the line causing error
return cell
}
Run Code Online (Sandbox Code Playgroud)
这是我的PhotoCollectionViewCell课
class PhotoCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView! //I double checked this IBOutlet whether it connects with Storyboard or not
}
Run Code Online (Sandbox Code Playgroud)
现在是有趣的部分.
我使用的是prototype电池在UICollectionView和我设置了reusable identifier从属性检查器.我还将自定义类从身份检查器更改为我的PhotoCollectionViewCell.
我搜索了同样的问题,发现当使用原型单元格时,删除
self.collectionView!.register(PhotoCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
Run Code Online (Sandbox Code Playgroud)
从代码将工作.我试了一下它的确有效.
但我很想知道这个问题背后的原因.我无法重现同样的问题,
UITableView但有UICollectionView.
Swift中的这个UICollectionView的单元registerClass是关于如何注册类的UICollectionView.但我的问题并不指望如何注册.我的问题是关于一个与UITableView课堂不同但UICollectionView仅有的问题.我期待这个相互矛盾的问题之间存在实际差异.
Sul*_*han 16
注册单元格有3种方法(或者用于UITableView或UICollectionView).
你可以注册一个班级.这意味着单元格没有笔尖或故事板.不会加载UI并且没有连接出口,只会自动调用类初始化程序.
您可以注册UINib(即xib文件).在这种情况下,单元UI从xib加载并且连接出口.
您可以在故事板中创建原型单元格.在这种情况下,单元格会自动注册该特定的UITableViewController或UICollectionViewController.任何其他控制器都无法访问该单元格.
这些选项无法合并.如果故事板中有单元格原型,则无需再次注册,如果这样做,则会破坏故事板连接.
| 归档时间: |
|
| 查看次数: |
1777 次 |
| 最近记录: |