2009-10-11 5 views
3

J'ai une tableview avec plusieurs sections. J'aimerais pouvoir déplacer des lignes d'une section à une autre et supprimer une section une fois qu'elle n'a pas de ligne. J'essaye de faire ceci par l'intermédiaire de moveRowAtIndexPath mais le code que j'ai ne fonctionne pas et lève une exception de NSRangeException.moveRowAtIndexPath: comment supprimer une section une fois la dernière ligne déplacée vers une autre section

Voici un exemple de code:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 

    NSUInteger fromSection = [fromIndexPath section]; 
    NSUInteger fromRow = [fromIndexPath row]; 
    NSString *fromKey = [self.keys objectAtIndex:fromSection]; 
    NSMutableArray *fromEventSection = [self.eventsDict objectForKey:fromKey]; 

    NSUInteger toSection = [toIndexPath section]; 
    NSUInteger toRow = [toIndexPath row]; 
    NSString *toKey = [self.keys objectAtIndex:toSection]; 
    NSMutableArray *toEventSection = [self.eventsDict objectForKey:toKey]; 

    id object = [[fromEventSection objectAtIndex:fromRow] retain]; 
    [fromEventSection removeObjectAtIndex:fromRow]; 
    [toEventSection insertObject:object atIndex:toRow]; 
    [object release]; 
    // The above code works just fine! 

    // Try to delete an empty section. Here is where trouble begins: 
    if ((fromSection != toSection) && [fromEventSection count] == 0) { 
     [self.keys removeObjectAtIndex:fromSection]; 
     [self.eventsDict removeObjectForKey:fromKey]; 

     [tableView deleteSections:[NSIndexSet indexSetWithIndex:fromSection] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
+0

Avez-vous obtenu une réponse à cela. Je ne peux pas comprendre non plus –

+0

Pas exactement. Mais j'ai travaillé autour de lui comme suit: J'ai arrêté d'essayer de supprimer les sections vides pendant que l'utilisateur déplace des lignes autour et a plutôt choisi de supprimer les sections vides une fois que l'utilisateur a fini avec l'édition. Cependant, même alors, la suppression et la section vide ne fonctionnaient pas correctement (je suspecte un bug d'Apple). Donc, afin de supprimer les sections vides, j'ai fini par ajouter une rangée de hauteur zéro dans chacune de mes sections vides, puis en supprimant les sections. Jetez un oeil à cette démo sur YouTube: http://tinyurl.com/ycg4uhs pour voir si cette approche fonctionnerait pour vous. – Sergio

Répondre

3

j'ai eu la chance d'effectuer le procédé de deleteSections dans un bloc ultérieur à la fin du procédé de moveRowAtIndexPath en enveloppant le retrait dans un dispatch_async.

dispatch_async(dispatch_get_main_queue(), ^{ 
     if ((fromSection != toSection) && [fromEventSection count] == 0) { 
      [self.keys removeObjectAtIndex:fromSection]; 
      [self.eventsDict removeObjectForKey:fromKey]; 
      [tableView deleteSections:[NSIndexSet indexSetWithIndex:fromSection] withRowAnimation:UITableViewRowAnimationFade]; 
     } 
    }); 
+0

Cela fonctionne bien. Je vous remercie! – akw

0

Cela m'a aussi causé du chagrin. J'ai réussi à effectuer la suppression de section en utilisant un délai.

Voici comment je l'ai appris à travailler - en supposant que vous utilisez un magasin pour contenir tous les objets, et le magasin a des méthodes pour déplacer les éléments:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 
    NSInteger beforeSectionCount = [store sectionCount]; 
    [store moveObject:fromIndexPath toIndexPath:toIndexPath]; 
    if (beforeSectionCount > [store sectionCount] 
     [self performSelector:@selector(deleteSection:) withObject:fromIndexPath: afterDelay:0.2] 
} 

- (void)deleteSection:(NSIndexPath *)indexPath { 
    [[self tableView] beginUpdates]; 
    [[self tableView] deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] 
       withRowAnimation:UITableViewRowAnimationFade]; 
    [[self tableView] endUpdates]; 
} 
Questions connexes