使用Blog JSON数据填充表视图

时间:2014-04-16 00:25:19

标签: ios json uitableview

所以这是我的代码:

TableViewController.h

@interface TableViewController : UITableViewController

@property (nonatomic, strong) NSArray *blogPosts;

@end

TableViewController.m

   - (void)viewDidLoad
    {
        [super viewDidLoad];

        NSURL *blogURL = [NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary/"];

        NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

        NSError *error = nil;

        NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                       options:0 error:&error];



        self.blogPosts  = [dataDictionary objectForKey:@"posts"];
}

在日志中显示

2014-04-15 20:21:48.884 BlogReader [772:60b]无法找到CFBundle 0xa181ef0的可执行文件(未加载)

1 个答案:

答案 0 :(得分:0)

我忘了确保作者和标题字符串与JSON中的字符串相匹配

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSDictionary *blogPost = [self.blogPosts objectAtIndex:indexPath.row];

    // Extracting 'Title' and 'Author' from dictionary to display in cell
    cell.textLabel.text = [blogPost valueForKey:@"title"];

    cell.detailTextLabel.text = [blogPost valueForKey:@"author"];

    return cell;
}