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];
}
Avez-vous obtenu une réponse à cela. Je ne peux pas comprendre non plus –
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