2017-08-28 22 views
2

viewDidLoad我這樣註冊細胞:UICollectionView.dequeueReusableCell崩潰

let cellIdentifier = "Cell" 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let cellNib = UINib(nibName: "ViewCell", bundle: nil) 
    collection.register(cellNib, forCellWithReuseIdentifier: cellIdentifier) 
} 

,並在UICollectionViewDataSourcecellForItemAt我做的:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    // It crashes while trying to dequeue with the Error message: Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] 
    let dequedCell = collection.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) 
    return dequedCell 
} 

的onlystrange事情可能是代碼混合斯威夫特/ ObjC,並且ViewCell是ObjC並且被導入到橋接頭中:

#import "ViewCell.h" 

我收到以下錯誤:

*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewC‌​ategory:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UI‌​Kit-3600.7.47/UIColl‌​ectionView.m:5106

我確信所有名稱是正確的。有誰知道爲什麼這個崩潰正在發生?

+1

您能添加確切的錯誤日誌嗎? –

+0

在[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:],/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.7.47/UICollectionView中發生'***聲明失敗的確切錯誤。 m:5106' – Daniel

+0

您確定拼寫正確的筆尖名稱嗎?再次檢查。我有一種情況,其中文件名字符串包含一些不可見的字符。 –

回答

1

這裏是問題

1)您需要註冊在XIB小區標識符,

2)分配的類名稱來XIB

3),用於細胞你需要檢查與行? YourClass in dequeue and

4)與您分配給XIB的相同標識符的出列隊列和寄存器

0

將collectionViewview單元格標識符設置爲故事板中的單元格。

enter image description here

+0

分配單元格標識符不需要使用自定義筆尖單元格。 – vaibhav

+0

我的故事板中的集合視圖沒有可重用的集合視圖。 – Daniel

0

Does anyone know why this crash is happening?

有一點要考慮的是,對於the docsregister(_:forCellWithReuseIdentifier:)

The nib file must contain only one top-level object and that object must be of the type UICollectionViewCell.

因此,檢查這是真的。換句話說...

let nibObjects = cellNib.instantiate(withOwner: nib, options: nil) 
let count = nibObjects.count // is this 1? 
let view = nibObjects.first // is view a UICollectionViewCell?