Упорядочивание секций UITableView, загруженных из списка pList - PullRequest
2 голосов
/ 02 октября 2011

У меня секционированный UITableView, заполняемый данными из списка pList.Корнем списка pList является словарь, каждый ключ этого словаря загружается в массив, который используется для определения заголовков разделов (и предположительно порядка).

В настоящее время мои разделы появляются в таблице случайным образом.порядок, я хотел бы упорядочить их, как они появляются в списке pList, или, по крайней мере, подделать его, упорядочив разделы в алфавитном порядке.

Я понимаю, что словари не имеют порядка, поэтому вывод не отражает pListпорядок, но я бы предпочел не менять корень на массив, так как это требует от меня переписывать куски моего приложения, плюс Xcode 4, кажется, предпочитает использовать словарь в качестве корня.Конечно, если изменение структуры pList - лучший метод, я пойду с этим.

Пример pList:

<?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>

Код в 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)numberOfSectionsInTableView:(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"];
}

1 Ответ

3 голосов
/ 02 октября 2011

Я могу думать о двух вариантах:

  1. Использовать массив в качестве корня вашего plist.Каждый элемент в массиве будет словарём с двумя ключами: name / title и items.Имя / заголовок будет строкой, а элементы - массивом.

  2. Сохраните свой список как есть, упорядочите ключи по алфавиту:

    NSArray *sortedKeys = [[dict allkeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]];
    
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...