2013-02-15 86 views
1

我設法創建了一個ArrayMyLocation對象,其中的距離屬性設置在tabBarController的2個選項卡中的第一個中。在我的第二個選項卡中,我得到它像這樣在viewDidLoad:此行之後如何爲陣列工作制作NSSortDescriptor?

[self setAnnotationsToSort:myDelegate.annotationsToSort]; 

,右我調用這個方法:

-(void)sort{ 
    NSSortDescriptor *sortDescriptor; 
    sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES] autorelease]; 
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 
    NSArray *sortedArray; 
    sortedArray = [self.annotationsToSort sortedArrayUsingDescriptors:sortDescriptors]; 
    [self.tableView reloadData]; 
} 

應該排序我Array。但是,我的tableView當前加載了Array,self.annotationsToSort的未排序版本。如何重新加載tableView但強制它使用新的sortedArray

回答

5

你沒有做任何事情sortedArray。嘗試

self.annotationsToSort = [self.annotationsToSort sortedArrayUsingDescriptors:sortDescriptors]; 
+0

是的,我嘗試過,但appartently方法sortedArrayUsingDescriptors返回一個NSArray和我self.annotationsToSort是NSMutableArray – marciokoko 2013-02-15 23:38:41

+0

只是讓它成爲一個'mutableCopy',所以你可以將其存儲爲'NSMutableArray' – 2013-02-16 00:01:10

2

電話:

self.annotationsToSort = sortedArray;

在打電話前[self.tableView reloadData];

0

我錯過了這一步:

self.annotationsToSort = [(NSArray*)sortedArray mutableCopy]; 

謝謝你們