Прочитайте и напишите мой Plist, у которого есть Массив Словаря, используя Xcode - PullRequest
1 голос
/ 18 июля 2011

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

Если бы вы могли помочь, предоставив хороший учебник или примеры кодов, было бы замечательно. Спасибо!

<array>
    <dict>
        <key>Name</key>
        <string>Device 2</string>
    </dict>
    <dict>
        <key>Name</key>
        <string>Device 1</string>
    </dict>
</array>

Эти коды на мой взгляд DidLoad ..

NSBundle *bundle = [NSBundle mainBundle];

    NSString *path = [bundle pathForResource:@"devicelist" ofType:@"plist"];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSLog(@"The file exists");
    } else {
        NSLog(@"The file does not exist");
    }

    contentDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    NSLog(@"The count: %i", [contentDict count]);
    contentArray = [NSMutableArray arrayWithContentsOfFile:path];
    NSLog(@"The array: %i", [contentArray count]);
    [contentArray retain];
    [contentDict retain];
    [mainTableView reloadData];

и это в моем tableView ..

static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]autorelease];
    }

    cell.textLabel.text = [sortedArray objectForKey:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;

1 Ответ

4 голосов
/ 18 июля 2011

Может быть, вы хотите попробовать следующее в viewDidLoad:

NSBundle *bundle = [NSBundle mainBundle];

    NSString *path = [bundle pathForResource:@"devicelist" ofType:@"plist"];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSLog(@"The file exists");

    self.contentArray = [NSMutableArray arrayWithContentsOfFile:path];
    NSLog(@"The array: %i", [contentArray count]);

    [mainTableView reloadData];
}

просто обратите внимание, что contentArray должен быть свойством retain, а затем в методе cellForRowAtIndexPath:

NSDictionary *dict = [self.contentArray objectAtIndex:indexPath.row];
cell.textLabel.text = (NSString*)[dict objectForKey:@"Name"];

Надеюсь, это поможет...

...