заполнить NSArray в соответствии со значением числа в списке NSArray - PullRequest
0 голосов
/ 14 марта 2012

Я хотел бы заполнить свой NSArray в соответствии с числом в списке свойств NSArray

Что я точно хочу, это: Если (один из тегов isEqualToNumber '2' в первом NSArray, тогда я получу ключNSArray)

Это мой список:

<?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>A</key>
    <array>
        <dict>
            <key>name</key>
            <string>Alban</string>
            <key>place</key>
            <string>Paris</string>
            <key>tag</key>
            <integer>2</integer>
        </dict>
    </array>
    <key>B</key>
    <array>
        <dict>
            <key>name</key>
            <string>Bernard</string>
            <key>place</key>
            <string>Paris</string>
            <key>tag</key>
            <integer>2</integer>
        </dict>
    </array>
    <key>C</key>
    <array>
        <dict>
            <key>name</key>
            <string>Clement</string>
            <key>place</key>
            <string>Paris</string>
            <key>tag</key>
            <integer>3</integer>
        </dict>
    </array>
    <key>D</key>
    <array>
        <dict>
            <key>name</key>
            <string>Dominique</string>
            <key>place</key>
            <string>Paris</string>
        </dict>
    </array>
    <key>E</key>
    <array>
        <dict>
            <key>name</key>
            <string>Edouard</string>
            <key>place</key>
            <string>Paris</string>
        </dict>
    </array>
    <key>F</key>
    <array>
        <dict>
            <key>name</key>
            <string>Francois</string>
            <key>place</key>
            <string>Paris</string>
            <key>tag</key>
            <integer>3</integer>
        </dict>
    </array>
    <key>G</key>
    <array>
        <dict>
            <key>name</key>
            <string>Guillaume</string>
            <key>place</key>
            <string>Paris</string>
        </dict>
    </array>

    [...]

    <key>Z</key>
    <array>
        <dict>
            <key>name</key>
            <string>Zyed</string>
            <key>place</key>
            <string>Paris</string>
        </dict>
    </array>
</dict>
</plist>

Это мой код:

.h ->

@interface StartUpTableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
{
    NSDictionary *names;
    NSArray *keys;
    NSInteger selectedButton;

}

.m ->

- (void)viewDidLoad
{
    NSLog(@"%@", [NSString stringWithFormat:@"Your button was %d", selectedButton]);

    NSString *path = [[NSBundle mainBundle] pathForResource:@"StartUps" ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    self.names = dict;


    NSArray *array = [[names allKeys] sortedArrayUsingSelector:@selector(compare:)];

    int i, count;
    id key, value;

    keys = [dict allKeys];
    count = [keys count];
    for (i = 0; i < count; i++)
    {
        key = [keys objectAtIndex: i];
        value = [dict objectForKey: key];
    }

    NSLog(@"%@", keys);
    self.keys = array;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];
    NSString *key = [keys objectAtIndex:section];

    NSArray *nameSection = [names objectForKey:key];

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];    }

    if ([[[nameSection objectAtIndex:row] valueForKey:@"tag"] isEqualToNumber:[NSNumber numberWithInt:selectedButton]])
    {
        cell.textLabel.text = [[nameSection objectAtIndex:row] valueForKey:@"name"];
        cell.imageView.image = [UIImage imageNamed:[[nameSection objectAtIndex:row] valueForKey:@"image"]];
    }
        return cell;

}

То, что у меня сейчас есть: таблица с именами разделов A, B, C, D, E, ... но с некоторыми пробелами ...

Что я хочу (так в соответствии со значением ключевого тега) ... табличное представление с именем SectionNames в качестве имени NSArray, имеющее в своем словаре ключевой тег, который в качестве значения «2» (например, 2, может быть 1, 2, 3, 4)

Спасибо за помощь:)

...