にNSMutableArray *rows;
データを初期化して入力する がありますviewDidLoad
。その時点で、明らかにデータがあります。この場合、3 つのエントリ。
それからtableView:cellForRowAtIndexPath
私は呼んで[rows objectAtIndex:indexPath.row]
います。ただし、この時点では、rows
配列にはまだ 3 つのエントリが含まれていますが、これらのエントリの値0x00000000
は元の値ではありません (たとえば、'id' は以前は でし12345
たが、現在は0x00000000
.
どういうわけか のデータの値がとのrows
間のどこかで空になっているように思えます。何が原因でしょうか?viewDidLoad
tableView:cellForRowAtIndexPath
編集
コードは次のとおりです。
ViewController.m
:
@implementation ViewController
NSMutableArray *rows;
- (void)viewDidLoad
{
rows = [[NSMutableArray alloc] init];
[rows setArray:myData]; // myData is als an NSMutableArray populated from JSON data.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
User *user = [rows objectAtIndex:indexPath.row]; // At this point 'rows' contains three entries but the values are empty.
}
@end
編集2
いくつかの提案された変更後のコードは次のとおりです。
ViewController.m
@interface ViewController()
{
NSMutableArray *rows;
}
@implementation ViewController
- (void)setRowsFromJSON
{
NSString *fileContents = [NSString stringWithContentsOfFile:@"data.json" encoding:NSUTF8StringEncoding error:nil];
NSData *jsonData = [fileContents dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
rows = [NSMutableArray arrayWithCapacity:[jsonArray count]];
User *user;
for (NSDictionary *aUser in jsonArray) {
user = [[User alloc] init];
user.id = [aUser valueForKey:@"id"];
user.name = [aUser valueForKey:@"name"];
[rows addObject:user];
}
}
- (void)viewDidLoad
{
[self setRowsFromJSON];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
User *user = [rows objectAtIndex:indexPath.row]; // At this point 'rows' contains three entries but the values are empty.
}
@end