2012-02-03 9 views
1

私は、次の内容でのplist(images.plist)を持っているロードplistの

images.plist

あなたが見ることができるように、各項目は0-19からの数字キーを持っています。各項目には2つの文字列(fileNameとfileInfo)もあります。

私はすべてのfileNameをTableViewにロードしようとしています。ここに私の試みです:

RosterMasterViewController.h

@interface RosterMasterViewController : UITableViewController 

@property (nonatomic, strong) NSDictionary *roster; 

@end 

RosterMasterViewController.m

@implementation RosterMasterViewController 

@synthesize roster = _roster; 

... 
// This is in my 'viewDidLoad' 
NSString *file = [[NSBundle mainBundle] pathForResource:@"images" ofType:@"plist"]; 
self.roster = [NSDictionary dictionaryWithContentsOfFile:file]; 

は、そして、ここで私はプロトタイプ細胞にファイル名をロードしようとしている方法です。レコードの

RosterMasterViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"imageNameCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell 
    cell.textLabel.text = [[[self.roster allKeys] objectAtIndex:indexPath.row] objectForKey:@"fileName"]; 
    return cell; 
} 

NOTE 私は@"HELLO!"するcell.textLabel.textを設定した場合、私のCellIdentifierが正しいことを、私は "HELLO!" が表示されますNSDictionaryの各項目について//Configure the cellセクションで問題が発生しています

残念ながら、これは期待通りに機能しません。私の鍵はすべて数値だと思うので、私は苦労している。

// Configure the cell 
NSLog(@"Key: %@", [NSNumber numberWithInt:indexPath.row]); 
NSDictionary *dict = [self.roster objectForKey:[NSNumber numberWithInt:indexPath.row]]; 
NSLog(@"Dictionary: %@", dict); 
NSString *fileName = [dict objectForKey:@"fileName"]; 
NSLog(@"FileName: %@", fileName); 

cell.textLabel.text = fileName; 
return cell; 

しかし、それは私のような結果与えている:

2012-02-03 11:24:24.295 Roster[31754:f803] Key: 7 
2012-02-03 11:24:24.295 Roster[31754:f803] Dictionary: (null) 
2012-02-03 11:24:24.296 Roster[31754:f803] FileName: (null) 

た場合に、私は以下の回答から学んだものを使用しようとすると

UPDATE

は、私はこれを持っています私はこの行を変更します:

NSDictionary *dict = [self.roster objectForKey:[NSNumber numberWithInt:indexPath.row]]; 

NSDictionary *dict = [self.roster objectForKey:@"5"]; 

その後、細胞の全ては、第六の要素のための正しいファイル名を持つことになります。 [NSNumber numberWithInt:indexPath.rowがうまくいかない理由は何ですか?

答えて

2

あなたはこれを行うことができます:それはNSDictionaryのをウィッヒは、各数字キーのための彼の辞書の内側に持っているのですself.roster

NSDictionary *dict = [self.roster objectForKey:indexPath.row]; 
NSString *fileName = [dict objectForKey:@"fileName"]; 
+0

私は 'self.roster = [NSDictionary dictionaryWithContentsOfFile:file];'が整数型キーで辞書を返すことを期待していますが、それぞれからfileNameにアクセスする方法をまだ理解していません。 – ardavis

+0

@ardavis私の更新された答えを見てください:) –

+0

ありがとう、それはうまくいくようですが、最初の行: 'NSDictionary * dict = [self.roster objectForKey:indexPath.row];'私にエラーを与えています。 ARCでは、 "NSInteger"(別名int)を 'id'にすることはできません。私はARCを使用しています、なぜ私はそれを行うことができないのか理解できますか? – ardavis

0

としては、オスカーによって指摘。

最初の数字キーのNSDictionaryのを取得する必要があります。その後NSDictionary *fileDictionary = [self.roster objectForKey:indexPath.row];

あなたは@「FILENAME」キーの文字列を要求する必要がありますので、あなたは、この最後の辞書からあなたのファイル名を抽出する必要があります。

NSString *fileName = [fileDictionary objectForKey:@"fileName"]; 
    cell.textLabel.text = fileName; 
    return cell; 
0

問題が既に解決されているかどうかはわかりませんが、この問題を解決する方法は次のとおりです。

NSDictionary *dict = 
[self.allItem objectForKey:[NSString stringWithFormat:@"%d",indexPath.row]]; 

私は理由が[NSNumber numberWithInt:indexPath.row]数/ int型の値を返すされたと思います。 ただし、objectForKey:は文字列値を受け取る予定です。

このヘルプが必要です。