2010-02-01 2 views
0

lorsque vous cliquez sur le updateButton mettre à jour le UITableView.NSInvalidArgumentException UITableView

.h 
    NSArray *sortedArray; 
    NSMutableArray *animals; 

    .m 
     -(void)update{ 
       [self checkAndCreateDatabase]; 
     [self readAnimalsFromDatabase]; 
     [self sort]; 
     [self.tableView reloadData]; 
     } 


     - (void)sort{ 
     NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES] autorelease]; 
     NSMutableArray *sortDescriptors = [NSMutableArray arrayWithObject:sortDescriptor]; 
     sortedArray = [animals sortedArrayUsingDescriptors:sortDescriptors]; 

     } 

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
.... 
//This works but it uses the animals, unsorted array. 
//If I replace it with this: Animal *myAnimal = (Animal *)[sortedArray objectAtIndex:indexPath.row];, it will give the error1. 

     Animal *myAnimal = (Animal *)[animals objectAtIndex:indexPath.row]; 
     NSString *temp = [NSString stringWithFormat:@"%@ - %@",myAnimal.distance, myAnimal.name]; 
     cell.textLabel.text = temp; 
     return cell; 
     } 

error1:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[_NSIndexPathUniqueTreeNode objectAtIndex:]: unrecognized selector sent to instance 0x3d3feb0' 

Toute aide serait génial! Je vous remercie.

Répondre

1

Vous avez probablement besoin de conserver votre tableau des animaux classés

sortedArray = [[animals sortedArrayUsingDescriptors:sortDescriptors] retain]; 

L'indice est que votre erreur dit [_NSIndexPathUniqueTreeNode objectAtIndex:] mais vous attend sortedArray être NSArray, non _ NSIndexPathUniqueTreeNode. Cela vous indique que l'objet sortedArray montre du doigt a changé, ce qui est généralement une indication qu'il a été libéré et la mémoire utilisée par quelque chose d'autre.

Sam

+0

un génie. Merci beaucoup. :) fait ma journée – hafhadg3