2010-02-01 45 views
0

當你點擊Updatebutton更新的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' 

任何幫助將是巨大的!謝謝。

回答

1

你可能需要保留您的分類的動物陣列

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

的線索就是你的錯誤說[_NSIndexPathUniqueTreeNode objectAtIndex:]但預計sortedArray是NSArray,不_ NSIndexPathUniqueTreeNode。這告訴你,sortedArray所指向的對象發生了變化,這通常是一個跡象表明,它已經釋放,別的東西所使用的內存。

山姆

+0

üR A天才。非常感謝你。 :)使我的一天 – hafhadg3 2010-02-02 13:39:08