Eliminar celdas de UICollectionView mediante NSNotification
Tengo una aplicación simple basada en UICollectionView - una UICollectionView y un modelo de datos basado en NSMutableArray para simplificar.
Puedo eliminar celdas sin problemas a través del método didSelectItemAtIndexPath: delegate:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
[self.data removeObjectAtIndex:[indexPath row]];
[self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
}
Sin embargo, estoy tratando de agregar una opción de eliminación a través de unaUIMenuController
en unUICollectionViewCell
subclase que se activa a través de unaUILongPressGestureRecognizer
que todo funciona bien y con éxito desencadenar unaNSNotification
-(void)delete:(id)sender{
NSLog(@"Sending deleteme message");
[[NSNotificationCenter defaultCenter] postNotificationName:@"DeleteMe!" object:self userInfo:nil];
}
Lo atrapo en mi ViewController y llamo al siguiente método:
-(void)deleteCell:(NSNotification*)note{
MyCollectionViewCell *cell = [note object];
NSIndexPath *path = nil;
if((path = [self.collectionView indexPathForCell:cell]) != nil){
[self.data removeObjectAtIndex:[path row]];
[self.collectionView deleteItemsAtIndexPaths:@[path]];
}
}
Y se bloquea en deleteItemsAtIndexPaths: call
-[UICollectionViewUpdateItem action]: unrecognized selector sent to instance 0xee7eb10
He comprobado todo lo que es obvio, como el objeto de NSNotification y el indexPath creado desde indexPathForCell: call y todo parece estar bien. Parece que estoy llamando a deleteItemsAtIndexPath: con la misma información en ambos lugares, pero por alguna razón falla cuando pasa por la ruta de notificación.
Esta es la información en la dirección dada en el error:
(lldb) po 0xee7eb10
(int) $1 = 250080016 <UICollectionViewUpdateItem: 0xee7eb10> index path before update (<NSIndexPath 0x9283a20> 2 indexes [0, 0]) index path after update ((null)) action (delete)
Quizás la ruta del índice después de la actualización sea nula sea significativa ...
¿Algunas ideas?