当您单击Update按钮以更新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;
}
错误1:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[_NSIndexPathUniqueTreeNode objectAtIndex:]: unrecognized selector sent to instance 0x3d3feb0'
任何帮助都会很棒!谢谢。
答案 0 :(得分:1)
您可能需要保留已排序的动物阵列
sortedArray = [[animals sortedArrayUsingDescriptors:sortDescriptors] retain];
线索是您的错误说[_NSIndexPathUniqueTreeNode objectAtIndex:]
,但您希望sortedArray为NSArray
,而不是_ NSIndexPathUniqueTreeNode
。这告诉你sortedArray指向的对象已经改变了,这通常表明它已经被释放并且其他东西使用了内存。
萨姆