Hai detto che stavi perdendo l'ordine delle chiavi, quindi suppongo che tu tocchi quelle chiavi da un NSArray
. Destra? E in quel NSArray
hai le chiavi ordinate come vuoi. E vedo anche che gli oggetti dello NSDictionary
sono Arrays
, giusto? Sì.Quindi nel tuo Ordered Array
hai più array.
data
è il nome del NSDictionary.
Quindi, tutto quello che dovete fare è portare lo NSArray
(quello che avete ordinato come sempre desiderato) in questo file .m e poi usare un codice fantastico nel vostro metodo cellForRowAtIndexPath
. È possibile farlo facendo quanto segue:
@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;
}
E questo è tutto quello che dovete fare per mantenere l'ordine che si desidera utilizzare un NSDictionary e NSArray.
Per visualizzare meglio, nel caso in cui l'array ordinato contiene solo le stringhe sarebbe come questo:
- (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;
}
Speranza che aiuta.
fonte
2014-03-15 00:44:49
Queste chiavi non sono mai state ordinate. I dizionari non hanno un ordine. –
possibile duplicato di [Come posso ottenere l'ordine originale di NSDictionary/NSMutableDictionary?] (Http://stackoverflow.com/questions/3386921/how-can-i-get-original-order-of-nsdictionary-nsmutabledictionary) – Amar