使用字符串填充tableview,这些字符串是嵌套到NSDictionary plist中的数组中的第二个元素[1]

时间:2013-09-24 00:22:46

标签: objective-c arrays ipad uitableview plist

我正在使用故事板开发iPad项目。 我试图用plist中的数据填充我的UITableView。 Plist有一个NSDictionary,它又有9个Arrays,每个有三个元素(一个数字和两个字符串)。我想从每个数组中获取第二个元素[1](这是一个字符串)并用这些字符串填充表。

来自头文件;

@interface EonOrderOfPrinciple : UIViewController
<UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *formManeuvers;
@property (nonatomic, strong) NSDictionary *Wild;
@property (nonatomic, strong) NSArray *Smash;
@property (nonatomic, strong) NSArray *CloseCombat;
@property (nonatomic, strong) NSArray *HeadButt;
@property (nonatomic, strong) NSArray *Pummel;
@property (nonatomic, strong) NSArray *Obstacle;
@property (nonatomic, strong) NSArray *Confusion;
@property (nonatomic, strong) NSArray *ImpWeap;
@property (nonatomic, strong) NSArray *Rage;
@property (nonatomic, strong) NSArray *WildStorm;

从实施;

NSString *aFile = [[NSBundle mainBundle]
                        pathForResource:@"Wild" ofType:@"plist"];
Wild = [[NSDictionary alloc] initWithContentsOfFile:aFile];

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:@"cell"];

NSString *currentManeuverName;
currentManeuverName = [Smash objectAtIndex:1];
[[cell textLabel] setText:currentManeuverName];
return cell;
}
@end

我意识到我现在只给它一个单元格。我想,一旦我上班,我就可以尝试让其余的人来填充。现在我甚至无法得到那个。

这些教程和我找到的帮助都没有解释如何使用这样的嵌套数据。我试图使用我发现的各种版本的东西,但没有任何作用。 在进入Core Data之前,我想学习如何使用plist。非常简单的plists我工作得很好,似乎是这个嵌套的业务让我绊倒。

* 10月29日更新...我现在已删除了plist *

SO。如何使用嵌套字典plist,正确访问信息,并填充表格。同样,这是使用嵌套字典。这是我不知道该怎么做,这就是我想要学习的方法。

4 个答案:

答案 0 :(得分:0)

回答问题的答案:

我认为您需要对如何构建PLIST进行更多反思。

在你的情况下,我认为做这样的事情会更好:

<强> Wild.plist

// Choose your key

<?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">
<array>
    <dict>
        <key>title</key>
        <string>Smash</string>
        <key>letter</key>
        <string>F</string>
        <key>id</key>
        <string>1</string>
    </dict>
    <dict>
        <key>title</key>
        <string>Close Combat</string>
        <key>letter</key>
        <string>W</string>
        <key>id</key>
        <string>2</string>
    </dict>
</array>
</plist>
你的.h

中的

@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
你的.m中的

// Your keys depend of your PLIST
#define KEY_TITLE @"title"
#define KEY_LETTER @"letter"
#define KEY_ID @"id"

// Data from your plist
@property (nonatomic, weak) NSMutableArray *data

- (void)viewDidLoad 
{
    [super viewDidLoad];    

    NSArray *a = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Wild" ofType:@"plist"]];

    self.data = [NSMutableArray array];

    for (NSDictionary *dic in a)
        [data addObject:[dic objectForKey:KEY_TITLE]; // Define your key
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:@"cell"];

    cell.textLabel = [data objectAtIndex:indexPath.row];

    return cell;
}

在玩之前的旧答案:

您需要在新的可变数组中获取PLIST文件的所有第二个元素,并将其声明为@property

@property (nonatomic, weak) NSMutableArray *data

在viewDidLoad中构建此数组:

(我认为您的PLIST带有第一个键的数组,请添加您的PLIST以获取更多信息)。

- (void)viewDidLoad 
{
    [super viewDidLoad];    

    NSArray *a = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Wild" ofType:@"plist"]];

    self.data = [NSMutableArray array];

    for (NSDictionary *dic in a)
        [data addObject:[dic objectForKey:KEY_SECOND_ELEMENT]]; // KEY_SECOND_ELEMENT is the key on your plist
}

接下来是您的表格视图:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:@"cell"];

    cell.textLabel = [data objectAtIndex:indexPath.row];

    return cell;
}

答案 1 :(得分:0)

首先,您需要将plist加载到字典中并从中提取数组:

NSString *plistFile = [[NSBundle mainBundle] pathForResource:@"Wild" ofType:@"plist"];
NSDictionary *Wild = [[NSDictionary alloc] initWithContentsOfFile:plistFile];

self.Smash = [Wild objectForKey:@"Smash"];
self.CloseCombat = [Wild objectForKey:@"CloseCombat"];
self.HeadButt = [Wild objectForKey:@"HeadButt"];
.
.
.

然后,您需要将该数据与UITableViewCell相关联:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                    initWithStyle:UITableViewCellStyleDefault
                    reuseIdentifier:@"cell"];
    }

    NSString *currentManeuverName;
    currentManeuverName = [Smash objectAtIndex:1];
    [[cell textLabel] setText:currentManeuverName];
    return cell;
}

@end

答案 2 :(得分:0)

我相信您想要将父级键作为节标题加载到部分和行中的plist!以下是通过3个简单步骤完成此操作的方法。

第1步:首先,您不需要很多属性来保存父键。只需添加两个类级变量,如下所示:

@interface EonOrderOfPrinciple () {
    NSDictionary    *parentDict;
    NSArray         *parentKeys;
}

第2步:viewDidLoad方法中填充这些类级变量:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *aFile = [[NSBundle mainBundle] pathForResource:@"Wild" ofType:@"plist"];

    parentDict = [NSDictionary dictionaryWithContentsOfFile:aFile];
    parentKeys = [parentDict allKeys];

    [self.formManeuvers reloadData];
}

第3步:然后在表数据源方法中加载它们:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return parentKeys.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return parentKeys[section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *parentKey     = parentKeys[section];
    NSDictionary *childDict = parentDict[parentKey];

    return [childDict allKeys].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell           = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSString *parentKey     = parentKeys[indexPath.section];
    NSDictionary *childDict = parentDict[parentKey];
    NSArray *childKeys      = [childDict allKeys];

    NSString *childKey   = childKeys[indexPath.row];
    cell.textLabel.text  = childDict[childKey][1];

    return cell;
}

答案 3 :(得分:0)

如果你在某个地方,我认为你正在加载数据 Smash = (NSArray*)[Wild objectForKey:@"WhateverKeyIsForWild"]; 在访问之前。

我认为您的问题不是嵌套字典或PLists或任何东西,因为您已经在这里和那里工作了。 (我将通过评论,而不是任何其他留言板)。

我相信你得到的是iOS。在访问阵列之前是否发生了加载功能?尝试通过在访问数据时记录其内容来查看Smash是否加载了数据。

NSLog(@"%@", Smash); // should automatically dump its contents into the logger

如果没有,零,垃圾,意外数据,那么你必须遵循你的代码,并确保首先加载,或者不收集内存。

尝试禁用ARC,您可以将代码复制并粘贴到没有它的新项目中。因为什么都不会改变(因为如果你只是测试一些字符串就可以泄漏)。如果它有效,你知道ARC就是它。

桌子的生命周期很有趣,有点烦人,因为你只能掌握一些关于操作顺序的东西。

嗯,这个答案比我想要的要长得多,我为此道歉。责备ARC。