UICollectionView cellForItemAtIndexPath no registra la celda
Estoy tratando de usar UICollectionViewCell
, ya que todo lo que quiero mostrar es una imagen. Puedo agregar la imagen a la celda usando UIColor colorWithImage:
en la propiedad UICollectionViewCell
's contentView
.
En mi método loadView
, estoy registrando la celda de la siguiente manera:
[self.collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"MyCell"];
A continuación se muestra mi cellForItemAtIndexPath
método:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
// cell customization
return cell;
}
Cuando lo corro, tan pronto como llega a la línea dequeue, se bloquea con el siguiente error:
*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:]
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier MyCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
Me cansé de configurar una celda personalizada, y la usé como la clase y obtuve el mismo error. Mi celular personalizado subclase UICollectionViewCell y no tenía nada implementado, excepto por el valor predeterminado initWithFrame
. Eso es porque quería cambiar el color de fondo de la vista. No estoy seguro de cuál es el problema, pero ¿podría alguien echar un vistazo a mi código y ayudarme? He estado tratando de resolver esto durante bastante tiempo sin absolutamente nada de suerte.
5 answers
Si solo desea mostrar una imagen, no necesita hacer ninguna subclase, puede establecer el backgroundColor
de la celda con colorWithPatternImage:
. Registre la clase así:
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
Entonces úsalo así:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithPatternImage:[self.results objectAtIndex:indexPath.row]];
return cell;
}
En este ejemplo, results es un array
de UIImages
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-18 10:23:31
Si está utilizando xib en applivation, agregue lo siguiente en su método viewDidLoad
[self.myCollectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CellIdentifier"];
De lo contrario, Si usa storyboard agregue lo siguiente
[self.myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CellIdentifier"];
Finalmente añadir esto (Si no)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath];
return cell;
}
La esperanza de arriba ayudará.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-14 05:13:48
Intente establecer un punto de interrupción en
[self.collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"MyCell"];
Supongo que tu loadView (¿te referías a viewDidLoad?) no se llama al método, por lo que la clase nunca se registra con collectionView.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-08 21:26:18
Si su vista de colección está conectada en storyboard y el delegado y la fuente de datos se establecen allí, y proporciona los métodos necesarios para la fuente de datos y el delegado, luego agregar la llamada de registro hace que el
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
Devuelve un UICollectionView
en lugar de tu propia subclase. Lo hacen bien, pero no tanto.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-25 12:22:30
Establezca el nombre de su identificador de celda como en el código
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-15 09:22:29