2012-02-12 20 views
16

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]; 
    } 
+0

¿Implementaron 'tableView commitEditingStyle: forRowAtIndexPath:' e implementaron el caso 'UITableViewCellEditingStyleInsert' para actualizar su origen de datos y realmente mover la fila (o' reloadData')? – lnafziger

+0

Uso core data y fetchedResultController para esto y toda la parte de guardado se hace en '- (void) tableView: moveRowAtIndexPath: toIndexPath:' – sarunw

+0

Bien, ahora que hay más código: – lnafziger

Respuesta

18

fin he encontrado el problema debido a su biblioteca externa que utilizo, IIViewDeckController, Después de eliminar que el problema desaparezca.

+5

Si desea mantener 'IIViewDeckController', puede corrige este problema al deshabilitar el modo de panorámica: 'viewDeck.panningMode = IIViewDeckNoPanning;' – rbrown

+0

Acabo de tener el mismo problema, y ​​esto resolvió mi problema. Eres mi héroe. – JRod

+3

Tuve el mismo problema con '' ECSlidingViewController''. Al eliminar temporalmente el gesto de deslizar con '' [self.view removeGestureRecognizer: self.slidingViewController.panGesture]; '' resolvió este problema para mí. – smek

3

Esto se debe a ViewDeckController. Estoy usando un puerto en MonoTouch y todavía encuentro el problema. Se debe (más específicamente) al UIPanGestureRecognizer. El reconocedor de gestos capta el movimiento de panorámica/arrastre y cree que es para sí mismo, por lo que cancela el toque que se envía a UITableView.

Lamentablemente, no existe una propiedad UIPanGestureRecognizer.direction (horizontal/vertical), pero hay una publicación aquí: UIPanGestureRecognizer - Only vertical or horizontal con respecto a cómo limitar el gesto de panorámica a una determinada dirección. Tendrá que modificar el código de ViewDeckController donde agrega el UIPanGestureRecognizer para usar la versión subclasificada de la pregunta anterior.

Aún no lo he probado (como las 2 de la mañana y listo para hoy) pero actualizaré mi respuesta una vez que haya tenido tiempo para implementar esto.

como una solución rápida y sucia, se puede establecer

panner.cancelsTouchesInView = NO; 

Esto le permitirá mover sus artículos, sin embargo, también tendrá su resultado previsto de no cancelar toques en la vista, por lo que, dependiendo de el contenido de su vista central, puede hacer que los usuarios presionen un botón cuando simplemente quieran deslizar la plataforma de visualización hacia un lado.

Otra posibilidad sería establecer esta propiedad en NO cuando establezca su tabla en modo de edición, y vuelva a cambiarla a SÍ cuando haya terminado.

Cuestiones relacionadas