Brodie
Brodie

Reputation: 3506

UITAbleView not in alphabetical order?iPhone

I have a tableView with several Sections being populated from a plist of NSDictionaries

How do I have it arrange the sections in the order they are in in the NSDictionary instead of alphabetically?

Upvotes: 0

Views: 878

Answers (2)

Brodie
Brodie

Reputation: 3506

I improvised a solution. I added a "01", "02", "03" etc to the beginning of each dictionary name then just added the code to remove those two characters before displaying it:

-(NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section{
    NSString *key = [keys objectAtIndex:section];
    key = [key substringFromIndex:2];
    return key;
}

Upvotes: 2

kennytm
kennytm

Reputation: 523384

NSDictionary is unordered. You should use an NSArray (or std::vector, or std::map, etc.) as the data source.

To get the keys, use -allKeys. To get a sorted array from it, use -sortedArrayUsingSelector:.

Upvotes: 3

Related Questions