2017-04-02 111 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或類名。