Ошибка утверждения iOS в UICollectionView
Я получаю сообщение об ошибке...
*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2372/UICollectionView.m:2249
При попытке отобразить UICollectionView.
Строки, вызывающие это,...
static NSString *CellIdentifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
Ошибка при удалении.
Нет никаких других ошибок, поэтому я изо всех сил пытаюсь понять, с чего начать.
Может ли кто-нибудь пролить свет на это?
Ответы
Ответ 1
Чтение документов (возможно, это было сделано первым:))
В любом случае, collectionView, который я использую, находится в отдельном файле xib (а не в виде раскадровки) и из документов...
Important: You must register a class or nib file using the
registerClass:forCellWithReuseIdentifier: or
registerNib:forCellWithReuseIdentifier: method before calling this method.
Спасибо
Ответ 2
Вам необходимо зарегистрироваться, как показано ниже:
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MY_CELL"];
Ответ 3
Убедитесь, что если вы используете метод registerNib:
:
UINib *nibH = [UINib nibWithNibName:HEADER_ID bundle:nil];
[collectionView registerNib:nibH
forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:HEADER_ID];
чтобы ТАКЖЕ в файле nib, когда вы выбираете вид многократного использования коллекции верхнего уровня, используйте инспектор атрибутов, а убедитесь, что установлен Identifier
то же значение, которое вы передаете параметру withReuseIdentifier:
.
Ответ 4
У меня была та же проблема. Вот как я это решил.
Переместить
[self.pictureCollectionView registerNib:[UINib nibWithNibName: bundle:nil] forCellWithReuseIdentifier:reuseID]
находится в - (void)viewDidLoad
,
а не метод - (void)awakeFromNib
.
Ответ 5
Заменить
NSString *CellIdentifier = @"Cell";
с
static NSString *CellIdentifier = @"Cell";
Ответ 6
Я видел, как эта ошибка появляется при использовании нескольких UICollectionView с уникальными ReuseIdentifiers. В ViewDidLoad вы хотите зарегистрировать каждый идентификатор ввода CollectionView следующим образом:
[_collectionView1 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collectionView1CellIdentifier"];
[_collectionView2 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collectionView2CellIdentifier"];
Затем, когда вы получаете "- (UICollectionViewCell *) collectionView: (UICollectionView *) collectionView cellForItemAtIndexPath: (NSIndexPath *) indexPath" вы хотите убедиться, что вы не пытаетесь установить ячейку для collectionView1 в reuseIdentifier для collectionView2 или вы получите эту ошибку.
НЕ ДЕЛАЙТЕ ЭТО: (Или 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;