2012-09-22 54 views
0

請原諒我,如果這已被回答。我遇到的所有答案都是使用相同的鍵對一系列字典進行排序。不是這種情況。使用UITableView(分組)的UIView具有tblData和部分。 tblData從一組對象中獲取數據。 我可以使用比較器對數組進行排序,但無論我嘗試過什麼,變化都不會反映出來。 這是創建數據的方法:對基於字典唯一鍵的NSDictionaries數組排序

if (myAppointment!=nil){ 
     if (myAppointment.OBSERVATIONS!=nil){ 
      [self.tblData removeAllObjects]; 
      @synchronized(self.myAppointment){ 
       for (Observations *tmpObs in [self.myAppointment OBSERVATIONS]){ 
        NSMutableString *tmpHdr = [[NSMutableString alloc] initWithString:@"Observation made on: "]; 
        [tmpHdr appendString:tmpObs.TIME]; 
        [self.tblData setObject:[[NSArray alloc] initWithObjects:tmpObs.TYPE, tmpObs.NOTE, nil] forKey:tmpHdr]; 
       } 
      } 
     } 
     self.sections = [[self.tblData allKeys] sortedArrayUsingSelector:@selector(compare:)]; 
    } 
    [self.GroupTblView reloadData]; 

當我嘗試了myAppointment.OBSERVATIONS排序,我可以驗證他們進行排序。然而,當我嘗試重新加載數據希望它已排序,沒有變化發生:

 [self.tblData removeAllObjects]; 
     [self.myAppointment.OBSERVATIONS sortUsingSelector:@selector(compareByType:)];     
     for (Observations *tmpObs in self.myAppointment.OBSERVATIONS){ 
      NSMutableString *tmpHdr = [[NSMutableString alloc] initWithString:@"Observation made on: "]; 
      [tmpHdr appendString:tmpObs.TIME]; 
      [self.tblData setObject:[[NSArray alloc] initWithObjects:tmpObs.TYPE, tmpObs.NOTE, nil] forKey:tmpHdr]; 
     } 
     self.sections = [[NSArray alloc] initWithArray:[self.tblData allKeys]]; 
     [self.GroupTblView reloadData]; 

這是我的理解是,部分沒有被排序,或者不被更新。 正如你從上面可以看到的那樣,字典關鍵字隨着每個「觀察」而改變,因此使用NSSortDescriptor並不像通常那樣簡單。 有沒有辦法解決這個問題,可以這麼說,使用block + sortedArrayUsingComparator?

編輯:

@property (strong, nonatomic) IBOutlet UITableView *GroupTblView; 
@property (nonatomic, strong) NSMutableDictionary *tblData; 
@property (nonatomic, strong) NSArray *sections; 

他們都合成並在initWithNib方法分配。

+0

您可以指定哪些是@properties以及它們是如何聲明的? – neilvillareal

+0

它真的覺得你正在嘗試重塑CoreData和NSFetchedResultsController。 –

+1

如果這是你所期望的,我不會在'OBSERVATIONS'的排序和'tblData'中的鍵的排序之間找到任何連接。無論如何安排插入,字典都沒有可預測的順序。 –

回答

0

那麼這個例子有點愚蠢,我把我的問題歸因於長時間的工作。若有人在排序中被組合模式下使用的UITableView的資料表曾經興趣,這裏是我是如何做的:

 self.sections = [[self.tblData allKeys] 
         sortedArrayUsingComparator: ^(id a, id b){ 
             // a is a section (a key from the Dictionary), b is another section (another key) 
             NSArray *a_arr = [self.tblData objectForKey:a]; 
             NSArray *b_arr = [self.tblData objectForKey:b]; 
             // We know that the first item in the array is always the type 
             return [[a_arr objectAtIndex:0] compare:[b_arr objectAtIndex:0]]; 
             }]; 
     [self.GroupTblView reloadData]; 

請記住,objectAtIndex:0是我observation.TYPE(見上面的代碼)。因此,如果您知道字典對象中數組排序鍵的索引,則可以訪問它,找到它(使用塊)並將其用於排序。