when you click on the Updatebutton to update the 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;
}
error开发者_JS百科1:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[_NSIndexPathUniqueTreeNode objectAtIndex:]: unrecognized selector sent to instance 0x3d3feb0'
Any help would be great! thank you.
You probably need to retain your sorted animals array
sortedArray = [[animals sortedArrayUsingDescriptors:sortDescriptors] retain];
The clue is that your error said [_NSIndexPathUniqueTreeNode objectAtIndex:]
but you expected sortedArray to be NSArray
, not _NSIndexPathUniqueTreeNode
. This tells you that the object sortedArray is pointing at has changed, which is usually an indication that it's been freed and the memory used by something else.
Sam
精彩评论