2017-04-02 113 views
-1

我在我的Collection视图电池连接到一个自定义的,但它崩溃,我已经写了自定义单元格的类名,创造了一流的,但是当我铸造dequeueReusableCell崩溃Collection视图细胞崩溃

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引用[1219:351279]无法将类型'UICollectionViewCell'(0x1b12070b0)的值转换为'Quote.PlatformCollectionViewCell'(0x1000bba00)。

我不知道是否可以在最后一行显示“NameOfTheProject.Class”。我觉得应该说只是类,但我不知道如何解决这个问题,在这种情况下是错误

类PlatformCollectionViewCell是这个

//

import UIKit 

class PlatformCollectionViewCell: UICollectionViewCell { 
    @IBOutlet weak var titleLabel: UILabel! 

} 

How I connected the PlatformCell

+0

什么是PlatformCollectionViewCell?你能表演那堂课吗?它应该是UICollectionViewCell的子类 –

+0

你可以分享如何注册你的细胞吗? 'reuseIdentifier' –

+0

我已经更新了问题 –

回答

1

错误消息告诉你到底发生了什么问题。对collectionView.dequeueReusableCell()的调用返回正常的UICollectionViewCell,而不是您的自定义单元的实例。

您需要在IB中打开我们的视图控制器,用您的标识符选择单元格原型,然后使用“身份检查器”来检查该单元格的类。我的猜测是,你忘了将班级切换到你的自定义单元班,PlatformCollectionViewCell。如果是这样,只需在身份检查器中更改UICollectionViewCell``UICollectionViewCell to PlatformCollectionViewCell`。

或者,您可以拨打register(_:forCellWithReuseIdentifier:)为您的重用标识符注册XIB或类名。