Estoy tratando de hacer que mi Core data respaldado UITableView tenga la capacidad de reordenar, después de implementar todos los delegados y alguna técnica para los datos centrales mencionados here encontré un comportamiento extraño. Después de presionar el botón Editar, reordenar ícono se muestra bien y puedo tocarlo, aparece la sombra, pero cuando trato de moverlo a otra fila, de repente pierdo el foco y la celda vuelve a su lugar. ¿Alguien sabe la causa de este problema?No se puede arrastrar UITableViewCell desde su posición actual al reordenar
Aquí está el vídeo que muestra el problema
http://www.youtube.com/watch?v=ugxuLNL7BnU&feature=youtu.be
Aquí está mi código
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
en -tableView:moveRowAtIndexPath:toIndexPath
Probé el código abajo y sólo una aplicación vacía, pero el problema todavía existen, así que no creo que esto no sea un problema.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
_changeIsUserDriven = YES;
NSUInteger fromIndex = sourceIndexPath.row;
NSUInteger toIndex = destinationIndexPath.row;
NSMutableArray *newsArray = [[self.fetchedResultsController fetchedObjects] mutableCopy];
News *news = [self.fetchedResultsController.fetchedObjects objectAtIndex:fromIndex];
[newsArray removeObject:news];
[newsArray insertObject:news atIndex:toIndex];
int i = 1;
for (News *n in newsArray) {
n.displayOrder = [NSNumber numberWithInt:i++];
}
[self saveContext];
_changeIsUserDriven = NO;
}
FetchedResultController delegado
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
if (_changeIsUserDriven) {
return;
}
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
if (_changeIsUserDriven) {
return;
}
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
if (_changeIsUserDriven) {
return;
}
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
if (_changeIsUserDriven) {
return;
}
[self.tableView endUpdates];
}
¿Implementaron 'tableView commitEditingStyle: forRowAtIndexPath:' e implementaron el caso 'UITableViewCellEditingStyleInsert' para actualizar su origen de datos y realmente mover la fila (o' reloadData')? – lnafziger
Uso core data y fetchedResultController para esto y toda la parte de guardado se hace en '- (void) tableView: moveRowAtIndexPath: toIndexPath:' – sarunw
Bien, ahora que hay más código: – lnafziger