1

ストーリーボードを使用して iPad のプロジェクトに取り組んでいます。UITableView に plist のデータを入力しようとしています。Plist には NSDictionary があり、これには 9 つの配列があり、それぞれに 3 つの要素 (1 つの数値と 2 つの文字列) があります。各配列 (文字列) から 2 番目の要素 [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

今は1つのセルしか与えていないことに気づきました。1 つが動作するようになったら、残りのデータを入力できるようになると考えました。今はそれさえも手に入れることができません。

このようにネストされたデータを使用する方法を説明しているチュートリアルも、私が見つけたヘルプもありません。見つけたもののさまざまなバージョンを使用しようとしましたが、何も機能しません。Core Data に移る前に、plist の使い方を学びたかったのです。私が作った非常に単純な plists は正常に機能します。このネストされたビジネスが私をつまずかせているようです。

* 10 月 29 日更新 ... plist を削除しました *

それで。ネストされたディクショナリ plist を使用して、情報に正しくアクセスし、テーブルにデータを入力する方法を教えてください。繰り返しますが、これはネストされた辞書を使用しています。これは私がどうすればよいか分からないことであり、これは私がどのようにすればよいかを学ぼうとしていることです。

4

4 に答える 4

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
于 2013-09-27T08:06:29.837 に答える
0

質問の Plist で答え​​てください :

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の前の古い回答:

PLIST ファイルのすべての 2 番目の要素を新しい可変配列で取得する必要があり、これを次のように宣言します@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;
}
于 2013-09-26T16:04:54.090 に答える
0

セクションヘッダーとして親キーを使用して、セクションと行に plist をロードしたいと思います! 3つの簡単なステップでそれを行う方法を次に示します。

ステップ 1:まず、親キーを保持するためだけに多くのプロパティは必要ありません。以下のように 2 つのクラス レベル変数を追加するだけです。

@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;
}
于 2013-10-02T04:57:46.807 に答える