私は、次の内容でのplist(images.plist)を持っているロード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
がうまくいかない理由は何ですか?
私は 'self.roster = [NSDictionary dictionaryWithContentsOfFile:file];'が整数型キーで辞書を返すことを期待していますが、それぞれからfileNameにアクセスする方法をまだ理解していません。 – ardavis
@ardavis私の更新された答えを見てください:) –
ありがとう、それはうまくいくようですが、最初の行: 'NSDictionary * dict = [self.roster objectForKey:indexPath.row];'私にエラーを与えています。 ARCでは、 "NSInteger"(別名int)を 'id'にすることはできません。私はARCを使用しています、なぜ私はそれを行うことができないのか理解できますか? – ardavis