ページ 11

objective-cでplistの内容をtableviewに表示させる

Posted: 2013年8月24日(土) 01:10
by vowvow123
objective-cでplistの内容をtableviewに表示させたいのですが表示されません。

辞書を作ろうと思っていてtableにPlistにあるアルファベットを表示させたいです。(今回はAとBのみだが辞書なのでZまで作る予定です)

以下のソースが自分で調べたり考えたりして作ってみた物ですがtableは表示されますが文字は表示されません。

ご教授願います。

Xcode4.6.3です。

[word.plist]

コード:

<plist version="1.0">
<array>
	<dict>
		<key>英語</key>
		<string>A</string>
		<key>意味</key>
		<string>アルファベットのA</string>
	</dict>
	<dict>
		<key>英語</key>
		<string>B</string>
		<key>意味</key>
		<string>アルファベットのB</string>
	</dict>
</array>
</plist>
[.h]

コード:

@interface TableViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> {
    
    __weak IBOutlet UITableView *word_table;
    NSArray *word_data;
    NSDictionary *allData;
}
@property (nonatomic, retain) NSDictionary *allData;
@property (nonatomic, retain) NSArray *word_data;
@end
[.m]

コード:

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSString* path = [[NSBundle mainBundle] pathForResource:@"word" ofType:@"plist"];
    word_data = [NSArray arrayWithContentsOfFile:path];
    
    word_table.dataSource = self;
    word_table.delegate = self;
    
    word_table.backgroundColor = [UIColor blueColor];
    word_table.rowHeight =30;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [allData count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *data = [allData allValues];
	
	return [[data objectAtIndex:section]count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:
(NSInteger)section{
	NSArray *dataTitle = [allData allKeys];
	return [dataTitle objectAtIndex:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
        
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont systemFontOfSize:40];
        
        cell.textLabel.text = [word_data objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.detailTextLabel.text=@"辞書";
        cell.detailTextLabel.textColor=[UIColor whiteColor];
    }
    
    return cell;
}

Re: objective-cでplistの内容をtableviewに表示させる

Posted: 2013年8月24日(土) 01:47
by h2so5
allDataにデータを入れる処理が見当たらないのですが、どこにあるのでしょうか?

Re: objective-cでplistの内容をtableviewに表示させる

Posted: 2013年8月24日(土) 03:00
by vowvow123
すいません。抜けていました。

allData = [NSDictionary dictionaryWithContentsOfFile:Path];

一番このデータを入れる所で苦慮しています。

知恵袋等で検索してみるとNSPropertyListSerializationを使ったらいい。という意見が多かったのでgoogleで調べてみましたが

日本語のページもあまりなく、英語のページもよくわかりませんでした。

初心者に毛が生えた程度の人間故、ケアレスミス等はご容赦下さい。

Re: objective-cでplistの内容をtableviewに表示させる

Posted: 2013年8月24日(土) 03:15
by h2so5
ここらへんのコードが参考になると思います。
http://tf.hateblo.jp/entry/2013/04/21/202545

plistの構造が「辞書の配列」になっているので、読み込むときはまず配列として読み込まないといけません。

Re: objective-cでplistの内容をtableviewに表示させる

Posted: 2013年8月24日(土) 20:11
by vowvow123
ありがとうございます!参考にさせて頂きます!