You said that you were losing the order of the keys, so I suppose you pull those keys from a NSArray
. Right? And in that NSArray
you have the keys ordered as you want. And I also see that the objects of the NSDictionary
are Arrays
, right? Yes. So in your Ordered Array
you have more arrays.
data
is the name of the NSDictionary.
So, all you have to do is to bring that NSArray
(the one that is ordered as you always wanted) into this .m file and after that use some awesome code in your cellForRowAtIndexPath
method. You can do it by doing the following:
@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;
}
And this is all you have to do to keep the order you want using a NSDictionary and NSArray.
To visualize it better, in the case that your ordered array only contains strings it would be like this:
- (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;
}
Hope it helps.