Setting NSMutableArray after sorting
I have a NSMutableArray of Marker objects that are shown in a UITableView. I have a segmentedControl to sort the data. I know my sorting by Name works when I NSLog my information like so:
// Create sort descriptors
NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray *sortDescripto开发者_JS百科rs = [[NSArray alloc] initWithObjects:descriptor, nil];
NSArray *sortedArray = [self.MarkersList sortedArrayUsingDescriptors:sortDescriptors];
for (Marker *m in self.MarkersList) {
NSLog(@"-Name: %@", m.Name);
}
for (Marker *m in sortedArray) {
NSLog(@"sorted - Name: %@", m.Name);
}
Then I don't know the best way to reload my data for the UITableView. I thought I could do the below, but that basically just removes all objects and now I have nothing in self.MarkersList.
if ([segmentControl selectedSegmentIndex] == 0) {
if (self.MarkersList != nil) {
[self.MarkersList removeAllObjects];
[self.MarkersList arrayByAddingObjectsFromArray:sortedArray];
[self.MarkersTableView reloadData];
NSLog(@"%s", __FUNCTION__);
for (Marker *m in self.MarkersList) {
NSLog(@"Name: %@", m.Name);
}
}
}
I'm definitely missing something obvious at this point. Any help would be appreciated. Thanks!
Replace:
[self.MarkersList arrayByAddingObjectsFromArray:sortedArray];
With:
[self.MarkersList addObjectsFromArray:sortedArray];
Spot the mistake and rejoice!
精彩评论