2010-04-30 4 views
1

J'ai un extrait de code qui ressemble à ceci:Obtenir une tableview insertRowsAtIndexPaths pour accepter indexOfObject?

[tableView beginUpdates]; 
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[array indexOfObject:[array objectAtIndex:indexPath.row]]] withRowAnimation:UITableViewRowAnimationLeft]; 
[tableView endUpdates]; 
[tableView reloadData]; 

Il est exécuté lorsqu'un utilisateur clique sur un accessoire. La première partie est seulement là pour fournir une animation fluide, et n'a pas vraiment d'importance, car le tableau est rechargé millisecondes plus tard, mais comme je l'ai dit, il est là pour fournir une animation.

Il est supposé déplacer un objet sélectionné de son indexPath actuel vers la valeur d'un tableau au même chemin d'index.

De toute évidence, ce code ne fonctionne pas, donc je veux juste savoir ce qui peut être fait pour le réparer? PS: Je reçois également un avertissement lors de la compilation. L'habitude « argument passant de 1 « arrayWithObject: » un entier en pointeur sans un casting ... » (ligne 3)

terminé avec cet extrait:

[tableView beginUpdates]; 
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[array indexOfObject:[arraySubFarts objectAtIndex:indexPath.row]] inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; 
[tableView endUpdates]; 

[tableView reloadData]; 

Répondre

1

Vous pouvez utiliser l'extension de classe NSIndexPath méthode +indexPathForRow:inSection: pour transformer une ligne en un chemin d'index. Plus de détails here.

Si votre seule intention avec la suppression et l'insertion de la ligne est de provoquer l'animation, avez-vous considéré la méthode reloadRowsAtIndexPaths:withRowAnimation:?

1
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[array indexOfObject:[array objectAtIndex:indexPath.row]]] withRowAnimation:UITableViewRowAnimationLeft]; 

Prendre cette ligne et la découper vous obtenez les suivantes:

NSObject *obj = [array objectAtIndex:indexPath.row]; 
int *index = [array indexOfObject:obj]; 
NSArray *otherArray = [NSArray arrayWithObject:index]; 
[tableView insertRowsAtIndexPaths:otherArray withRowAnimation:UITableViewRowAnimationLeft]; 

Qu'est-ce que vous voulez sans doute est:

NSObject *obj = [array objectAtIndex:indexPath.row]; 
NSIndexPath *index = [NSIndexPath indexPathForRow:[array indexOfObject:obj] inSection:0]; 
NSArray *otherArray = [NSArray arrayWithObject:index]; 
[tableView insertRowsAtIndexPaths:otherArray withRowAnimation:UITableViewRowAnimationLeft]; 

Mais pourquoi ne pouvez-vous faire cela?

NSArray *otherArray = [NSArray arrayWithObject:indexPath]; 
[tableView insertRowsAtIndexPaths:otherArray withRowAnimation:UITableViewRowAnimationLeft]; 

Obtenir l'objet du tableau à l'aide d'un index, puis en utilisant l'objet pour trouver l'index semble redondant. Utilisez simplement l'index.


Modifier avec plus de code:

NSNumber *obj = [NSNumber numberWithInt:indexPath.row]; 
int *index = [array indexOfObject:obj]; 
NSIndexPath *index = [NSIndexPath indexPathForRow:index inSection:0]; 
NSArray *otherArray = [NSArray arrayWithObject:index]; 
[tableView insertRowsAtIndexPaths:otherArray withRowAnimation:UITableViewRowAnimationLeft]; 
+1

la raison pour laquelle je fais ce que parce que le nouvel indice est au courant indexPath dans un tableau. (Si l'indexPath est 2, la valeur de 2 dans le tableau peut être 20). – Emil

+0

Ah, d'accord. Ce que le code que vous avez posté est de saisir l'objet à l'index 2, de rechercher l'index de l'objet à l'index 2 et de créer un tableau avec cet objet. J'ai ajouté un peu plus de code ci-dessus qui pourrait être plus proche de ce dont vous avez besoin. – MrHen

+0

Voir mis à jour le premier message, je l'ai réparé :) – Emil

Questions connexes