UICollectionView 上的 register(_:forCellWithReuseIdentifier:) 有什么问题? [英] What's wrong with register(_:forCellWithReuseIdentifier:) on UICollectionView?

查看:75
本文介绍了UICollectionView 上的 register(_:forCellWithReuseIdentifier:) 有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UICollectionView.正如 dequeueReusableCell(withReuseIdentifier:for:) 所期望的那样 在调用此方法之前,您必须使用 register(_:forCellWithReuseIdentifier:) 方法注册一个类或 nib 文件方法,我在 viewDidLoad 函数中添加了一行作为

I'm working with an UICollectionView. As dequeueReusableCell(withReuseIdentifier:for:) expects You must register a class or nib file using the register(_:forCellWithReuseIdentifier:) method before calling this method, I added a line in my viewDidLoad func as

self.collectionView!.register(PhotoCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

现在,当我使用单元进行出列和配置时,出现错误和应用程序崩溃.

Now when I'm using the cell for dequeuing and configuring, I'm getting error and app crashes.

致命错误:解包可选值时意外发现 nil

fatal error: unexpectedly found nil while unwrapping an Optional value

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

这是我的 PhotoCollectionViewCell

class PhotoCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var imageView: UIImageView! //I double checked this IBOutlet whether it connects with Storyboard or not
}


现在是有趣的部分.

我在 UICollectionView 中使用了一个 prototype 单元格,并从 设置了一个 可重用标识符>属性检查器.此外,我将自定义类身份检查器更改为我的PhotoCollectionViewCell.

I'm using a prototype cell in the UICollectionView and I set a reusable identifier from attributes inspector. Also I changed the custom class from identity inspector to my PhotoCollectionViewCell.

我搜索了同样的问题,发现在使用prototype cell时,删除

I searched for the same issue and found out that when using prototype cell, deleting

self.collectionView!.register(PhotoCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 

来自代码将起作用.我试了一下,效果很好.

from code will work. I gave it a try and it works.

但我很想知道这个问题背后的原因.我无法使用 UITableView 重现相同的问题,但使用 UICollectionView.

But I'm curious to know the reason behind this issue. I couldn't reproduce the same issue with UITableView but with UICollectionView.

不可能重复:

这个UICollectionView's cell registerClass in Swift是关于如何在UICollectionView.但我的问题并不期望如何注册.我的问题是关于一个与 UITableView 类无关但仅与 UICollectionView 相关的问题.我期待这个相互冲突的问题之间的实际差异.

Not a possible duplicate:

This UICollectionView's cell registerClass in Swift is about how to register class in UICollectionView. But my question doesn't expect how to register. My question is about an issue that isn't true with UITableView class but with UICollectionView only. I'm expecting the actual difference between this conflicting issue.

推荐答案

有 3 种方法可以注册单元格(UITableViewUICollectionView).

There are 3 ways to register a cell (either for UITableView or UICollectionView).

  1. 您可以注册一个课程.这意味着单元格没有笔尖或故事板.不会加载 UI,也不会连接任何插座,只会自动调用类初始化器.

  1. You can register a class. That means the cell has no nib or storyboard. No UI will be loaded and no outlets connected, only the class initializer is called automatically.

您可以注册一个UINib(即一个xib 文件).在这种情况下,单元 UI 从 xib 加载并连接出口.

You can register a UINib (that is, a xib file). In that case the cell UI is loaded from the xib and outlets are connected.

您可以在故事板中创建原型单元格.在这种情况下,单元格会自动为特定的 UITableViewControllerUICollectionViewController 注册.该单元在任何其他控制器中均不可访问.

You can create a prototype cell in the storyboard. In that case the cell is registered automatically for that specific UITableViewController or UICollectionViewController. The cell is not accessible in any other controller.

选项不能组合.如果故事板中有单元格原型,则无需再次注册,如果注册,则会断开故事板连接.

The options cannot be combined. If you have a cell prototype in the storyboard, you don't have to register again and if you do, you will break the storyboard connection.

这篇关于UICollectionView 上的 register(_:forCellWithReuseIdentifier:) 有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆