2013-07-19 57 views
9

我需要在UITableView中顯示由API返回的NSDictionary的內容,尊重鍵的順序。NSDictionary allKeys order

我使用:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     NSString *key = self.data.allKeys[indexPath.section]; 
     NSArray *list = self.data[key]; 
     id data = list[indexPath.row]; 

     PSSearchCell *cell = [PSSearchCell newCellOrReuse:tableView]; 

     cell.model = data; 

     return cell; 
} 

但我做self.data.allKeys,我失去了我的鑰匙的順序。我無法根據價值對它們進行分類,因爲它不涉及它們。

+1

這些鑰匙永遠不會在訂購地點。字典沒有訂單。 –

+0

可能重複的[我如何得到原始順序的NSDictionary/NSMutableDictionary?](http://stackoverflow.com/questions/3386921/how-can-i-get-original-order-of-nsdictionary-nsmutabledictionary) – Amar

回答

1

正如大家所說,我不能爲了我的鑰匙,因爲他們出現在我的NSDictionary日誌因爲放入NSDictionary沒有訂購。

我請人從API返回數組來代替:

0 => name : key 1 
    value : value 1 

1 => name : key 2 
    value : value 2 

/------------------------- -------------------------------------------------- -/

太糟糕了沒有使用這樣的一種方法「sortedArrayUsingArray」:

NSArray * arrayOne = [@"key E", @"key A", @"key C"]; 

NSArray * arrayTwo = [@"key A", @"key B", @"key C", @"key D", @"key E", @"key F"]; 

NSArray * orderedArray = [arrayOne sortedArrayUsingArray: arrayTwo]; 
+0

沒有這樣的方法存在:「sortedArrayUsingArray」。使用:http://blog.pioneeringsoftware.co.uk/2008/08/20/sorting-an-array-by-another-array/ –

23

試試這個,

NSArray *keys = [myDictionary allKeys]; 
keys = [keys sortedArrayUsingComparator:^(id a, id b) { 
    return [a compare:b options:NSNumericSearch]; 
}]; 

NSLog(@"%@",keys); 

現在基於排序鍵獲取值。

編輯

按字母順序嘗試這個排序,

NSArray *keys = [myDictionary allKeys]; 
keys = [[keys mutableCopy] sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

NSLog(@"%@",keys); 
+0

但我沒有「Foo1」,「Foo2」這樣的東西......我的密鑰名中沒有任何數字。我的API返回key =「Cinema」value =影院列表,key =「影院」,value =影院列表,我需要按順序顯示它們:首先是電影院,然後是影院。 –

+0

@NicolasRoy請檢查更新的答案。 –

+0

@βhargavḯ我不想對這些鍵進行排序,我希望在不改變順序的情況下接收鍵。那有可能嗎?請幫忙。 –

0

我寫了一個快速的方法採取源陣列(對象的s)和一個參考數組(它有一個期望的(並且完全是任意的)順序的對象),並且返回一個數組,其中源數組的項目已經被重新組織以匹配參考數組。

- (NSArray *) reorderArray:(NSArray *)sourceArray toArray:(NSArray *)referenceArray 
{ 
    NSMutableArray *returnArray = [[NSMutableArray alloc] init]; 
    for (int i = 0; i < [referenceArray count]; i++) 
    { 
     if ([sourceArray containsObject:[referenceArray objectAtIndex:i]]) 
     { 
      [returnArray addObject:[arrReference objectAtIndex:i]]; 
     } 
    } 
    return [returnArray copy]; 
} 

請注意,這是非常脆弱的。它使用NSArraycontainsObject:方法,最終將調用NSObject's isEqual:。基本上,它應該適用於NSString s,NSNumber s和NSDate s(還沒有嘗試過)的陣列,但除此之外,YMMV。我想如果你試圖傳遞UITableViewCell或者其他一些非常複雜的對象的數組,它會完全失敗,並且崩潰或者返回總垃圾。同樣,如果您要執行一些操作,例如將NSDate s作爲參考數組,並將NSString s作爲源數組。另外,如果源數組包含參考數組中未包含的項目,則它們將被丟棄。可以通過添加一些額外的代碼來解決其中的一些問題。所有這一切說,如果你想做一些簡單的事情,它應該很好地工作。在你的情況下,你只需發送arrayOne作爲源數組,arrayTwo作爲參考數組。

0

你說你失去了鑰匙的順序,所以我想你從NSArray拉那些鑰匙。對?並在那NSArray你有你想要的鍵排序。而且我也看到NSDictionary的對象是Arrays,對吧?是。所以在你的Ordered Array你有更多的數組。

data是NSDictionary的名稱。

所以,你所要做的就是將NSArray(按你想要的順序排列)放到這個.m文件中,然後在你的cellForRowAtIndexPath方法中使用一些很棒的代碼。您可以通過執行以下操作步驟:

@interface yourViewController() 
{ 
    NSArray *yourArrayOrdered; 
} 
@end 

@implementation yourViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    yourArrayOrdered = [[NSArray alloc] initWith...:... ]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath 
{ 
    NSString *key = yourArrayOrdered[indexPath.row]; 
    NSArray *list = [self.data objectForKey:yourArrayOrdered[indexPath.row]]; 

    //your code continues here... 

    /*id data = list[indexPath.row]; //don't think you need this line 

    PSSearchCell *cell = [PSSearchCell newCellOrReuse:tableView]; 

    cell.model = data; */ 

    return cell; 
} 

這是所有你必須做的,讓使用的NSDictionary的NSArray和你想要的順序。

爲了更好地觀察它,在你的有序陣列只包含字符串它會是這樣的情況:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath 
{ 
    NSString *key = yourArrayOrdered[indexPath.row]; 

    NSString *myString = [self.data objectForKey:yourArrayOrdered[indexPath.row]]; 

    cell.textLabel.text = [NSString stringWithFormat:@"THIS IS THE OBJECT %@", myString]; 

    cell.detailTextLabel.text = [NSString stringWithFormat:@"AND THIS IS THE KEY %@", key]; 

    return cell; 
} 

希望它能幫助。

相關問題