开发者

Ordering UITableView Sections Loaded From pList

开发者 https://www.devze.com 2023-04-10 06:50 出处:网络
I have a sectioned UITableView being populated by data from a pList. The root of the pList is a dictionary, each key of this dictionary is loaded into an array, which is used to define the section hea

I have a sectioned UITableView being populated by data from a pList. The root of the pList is a dictionary, each key of this dictionary is loaded into an array, which is used to define the section headings (and presumably order).

Currently my sections appear in the table in a random order, I would like to order them as they appear in the pList, or at least fake it by ordering the sections alphabetically.

I understand that dictionaries have no order, which is why the output does not reflect the pList order, but I would prefer not to change the root to an array as it requires me to rewrite chunks of my app, plus Xcode 4 seems to prefer you using a dictionary as the root. Of course if changing the pList structure is the best method, I'll go with that.

pList sample:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"   "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Section A</key>
    <array>
        <dict>
            <key>Details</key>
            <string>Details Here...</string>
            <key>Image</key>
            <string>X.png</string>
            <key>Name</key>
            <string>Person X</string>
        </dict>
        <dict>
            <key>Details</key>
            <string>Details Here...</string>
            <key>Image</key>
            <string>Y.png</string>
            <key>Name</key>
            <string>Person Y</string>
        </dict>
    </array>
    <key>Section B</key>
    <array>
        <dict>
            <key>Details</key>
            <string>Details Here...</string>
            <key>Image</key>
            <string>Z.png</string>
            <key>Name</key>
            <string>Person Z</string>
        </dict>
    </array>
</dict>

Code within 'TableViewController.m':

- (void)viewDidLoad {

[super viewDidLoad];

NSString *path = [[NSBundle mainBundle] pathForResource:@"Team" ofType:@"plist"];
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.teamDictionary = tempDict;
[tempDict release];

NSArray *tempArray = [[NSArray alloc] init];
self.teamArray = tempArray;
[tempArray release];

self.teamArray = [self.teamDictionary allKeys];
}

- (NSInteger)numbe开发者_Python百科rOfSectionsInTableView:(UITableView *)tableView {

return [self.teamArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [[self.teamDictionary valueForKey:[self.teamArray objectAtIndex:section]] count];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

// this is here because I am altering the appearance of the header text
UIView* customView = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, tableView.bounds.size.width, 44.0)] autorelease];

UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];

// some custom formatting stuff has been removed from here...

headerLabel.text = [self.teamArray objectAtIndex:section];
[customView addSubview:headerLabel];
[headerLabel release];

return customView; 
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [teamArray objectAtIndex:section];
NSArray *teamMember = [teamDictionary objectForKey:key];

static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionsTableIdentifier] autorelease];
}

// Configure the cell...

NSDictionary *name = [teamMember objectAtIndex:row];
cell.textLabel.text = [name objectForKey:@"Name"];
}


I can think of two options:

  1. Use an array as the root of your plist. Each item in the array would be a dictionary, with two keys: name/title and items. Name/title would be a string and items would be an array.

  2. Keep your plist as is, order the keys alphabetically:

    NSArray *sortedKeys = [[dict allkeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]];
    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号