我看到這個錯誤彈出時,使用獨特的ReuseIdentifiers多個UICollectionViews。在viewDidLoad中要註冊,像這樣各的CollectionView的reuseIdentifier:當你到
[_collectionView1 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collectionView1CellIdentifier"];
[_collectionView2 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collectionView2CellIdentifier"];
然後 「 - (UICollectionViewCell *)的CollectionView:(UICollectionView *)的CollectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath」 你要確保您不會嘗試將collectionView1的單元格設置爲collectionView2的reuseIdentifier,否則會出現此錯誤。
千萬不要這麼做:(或collectionView2會看到錯誤的標識,看標識就期待前大發脾氣)
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView1CellIdentifier" forIndexPath:indexPath];
if(collectionView != _collectionView1){
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView2CellIdentifier" forIndexPath:indexPath];
}
cell.backgroundColor = [UIColor greenColor];
return cell;
做到這一點:
UICollectionViewCell *cell;
if(collectionView == _collectionView1){
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView1CellIdentifier" forIndexPath:indexPath];
}else{
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView2CellIdentifier" forIndexPath:indexPath];
}
cell.backgroundColor = [UIColor greenColor];
return cell;
你什麼時候調用這個方法?在「UICollectionViewCell * cell = [collectionView dequeue ...」之前的行或在不同的方法 –
我必須註冊它在viewDidLoad方法。您只需要爲整個collectionView註冊一次xib。然後,當您調用dequeueCellWithIdentifier時,它會轉到您註冊的xib。 – Fogmeister
我有同樣的問題,但由於某種奇怪的原因,編譯器無法識別'viewDidLoad'中的'registerClass'方法,所以我不得不將它移動到'cellForItemAtIndexPath'方法。 –