我是 xcode 的新手,我需要帮助。
我有这个json:
{
"noticias": [
{
"titulo":"teste"
}
{
"titulo":"teste 2"
}
]
}
我是怎么得到结果的?我试过了
NSDictionary *not = [noticias objectAtIndex:indexPath.row];
但我得到了一个错误,因为我有关键的“noticias”
更新:这是尝试的代码,基于迄今为止收到的答案。我有这个:
- (void)fetchNoticias
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:
[NSURL URLWithString: @"http://www.site.com/json"]];
NSError* error;
noticias = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
}
然后
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchNoticias];
}
和我有问题的决赛
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NoticiasCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSArray *noticia = [noticias objectForKey:@"noticias"];
NSDictionary *not = [noticias objectAtIndex:indexPath.row];
NSString *firstTitle = [not objectForKey:@"titulo"];
cell.textLabel.text = firstTitle;
//cell.detailTextLabel.text = subtitulo;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
在线问题:
NSArray *noticia = [noticias objectForKey:@"noticias"];