集合视图单元格崩溃
Collection view Cell crashing
我正在将我的 Collection 视图单元格连接到一个自定义视图单元格,但它崩溃了,我已经编写了自定义单元格的 class 名称并创建了 class,但是当我投射 dequeueReusableCell 时它崩溃了
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PlatformCollectionViewCell
if let platformCell = cell as? PlatformCollectionViewCell {
platformCell.backgroundColor = UIColor.blue
platformCell.titleLabel.text = "Hola"
return cell
}
cell.backgroundColor = UIColor.green
return cell
}
我发现的唯一可能的错误是当它给我错误时它说的是
无法将类型 'UICollectionViewCell' (0x1b12070b0) 的值转换为 'Quote.PlatformCollectionViewCell' (0x1000bba00)。
2017-04-02 13:24:32.711435+0200 Quote[1219:351279] 无法将类型 'UICollectionViewCell' (0x1b12070b0) 的值转换为 'Quote.PlatformCollectionViewCell' (0x1000bba00).
我不知道最后一行写 "NameOfTheProject.Class" 是否可以。我认为它应该只说 class 但我不知道如何解决这个问题,如果这是错误
class PlatformCollectionViewCell 是这个
//
import UIKit
class PlatformCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var titleLabel: UILabel!
}
错误消息告诉您到底出了什么问题。对 collectionView.dequeueReusableCell()
的调用返回的是正常的 UICollectionViewCell
,而不是自定义单元格的实例。
您需要在 IB 中打开我们的视图控制器,select 带有您的标识符的单元格原型,并使用 "identity inspector" 检查该单元格的 class。我的猜测是您忘记将 class 切换为您的自定义单元格 class、PlatformCollectionViewCell
。如果是这样,只需在身份检查器中更改 UICollectionViewCell``UICollectionViewCell to
PlatformCollectionViewCell`。
或者,您可以调用 register(_:forCellWithReuseIdentifier:)
为您的重用标识符注册 XIB 或 class 名称。
Duncan 在 register
上的回答让我解决了这个问题。
在我创建自定义单元格之前,register
看起来像这样:
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
它在我创建自定义单元格后错过了更新。修复是:
self.collectionView!.register(MyViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
我最近遇到的一个问题,人们经常对此感到困惑。如果使用 xib 自定义 class,需要注册 xib 而不是 class,否则在 cellForItem 中访问的出口将为 nil 并崩溃。
self.collectionViewWeekCalendar.register(UINib(nibName: "CalenderCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CalenderCell")
我正在将我的 Collection 视图单元格连接到一个自定义视图单元格,但它崩溃了,我已经编写了自定义单元格的 class 名称并创建了 class,但是当我投射 dequeueReusableCell 时它崩溃了
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PlatformCollectionViewCell
if let platformCell = cell as? PlatformCollectionViewCell {
platformCell.backgroundColor = UIColor.blue
platformCell.titleLabel.text = "Hola"
return cell
}
cell.backgroundColor = UIColor.green
return cell
}
我发现的唯一可能的错误是当它给我错误时它说的是
无法将类型 'UICollectionViewCell' (0x1b12070b0) 的值转换为 'Quote.PlatformCollectionViewCell' (0x1000bba00)。 2017-04-02 13:24:32.711435+0200 Quote[1219:351279] 无法将类型 'UICollectionViewCell' (0x1b12070b0) 的值转换为 'Quote.PlatformCollectionViewCell' (0x1000bba00).
我不知道最后一行写 "NameOfTheProject.Class" 是否可以。我认为它应该只说 class 但我不知道如何解决这个问题,如果这是错误
class PlatformCollectionViewCell 是这个
//
import UIKit
class PlatformCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var titleLabel: UILabel!
}
错误消息告诉您到底出了什么问题。对 collectionView.dequeueReusableCell()
的调用返回的是正常的 UICollectionViewCell
,而不是自定义单元格的实例。
您需要在 IB 中打开我们的视图控制器,select 带有您的标识符的单元格原型,并使用 "identity inspector" 检查该单元格的 class。我的猜测是您忘记将 class 切换为您的自定义单元格 class、PlatformCollectionViewCell
。如果是这样,只需在身份检查器中更改 UICollectionViewCell``UICollectionViewCell to
PlatformCollectionViewCell`。
或者,您可以调用 register(_:forCellWithReuseIdentifier:)
为您的重用标识符注册 XIB 或 class 名称。
Duncan 在 register
上的回答让我解决了这个问题。
在我创建自定义单元格之前,register
看起来像这样:
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
它在我创建自定义单元格后错过了更新。修复是:
self.collectionView!.register(MyViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
我最近遇到的一个问题,人们经常对此感到困惑。如果使用 xib 自定义 class,需要注册 xib 而不是 class,否则在 cellForItem 中访问的出口将为 nil 并崩溃。
self.collectionViewWeekCalendar.register(UINib(nibName: "CalenderCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CalenderCell")