Powiedziałeś, że tracisz kolejność kluczy, więc przypuszczam, że wyciągasz te klucze z NSArray
. Dobrze? I w tym NSArray
masz klucze zamówione, jak chcesz. I widzę również, że obiekty NSDictionary
są Arrays
, prawda? Tak.Tak więc w twoim Ordered Array
masz więcej tablic.
data
to nazwa NSDictionary.
Wszystko, co musisz zrobić, to przynieść NSArray
(ten, który jest zamówiony, jak zawsze chciałeś) do tego pliku .m, a następnie użyć niesamowitego kodu w swojej metodzie cellForRowAtIndexPath
. Można to zrobić w następujący sposób:
@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;
}
I to jest wszystko, co musisz zrobić, aby utrzymać porządek chcesz przy użyciu NSDictionary i NSArray.
Aby uzmysłowić to lepiej, w przypadku, że zamówił tablica zawiera tylko ciągi to będzie jak ten:
- (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;
}
Nadzieję, że to pomaga.
Te klucze nigdy tam, gdzie zostały zamówione. Słowniki nie mają zamówienia. –
możliwy duplikat [Jak mogę uzyskać oryginalną kolejność NSDictionary/NSMutableDictionary?] (Http://stackoverflow.com/questions/3386921/how-can-i-get-original-order-of-nsdictionary-nsmutabledictionary) – Amar