【问题标题】:Populating table view with Blog JSON Data使用博客 JSON 数据填充表视图
【发布时间】:2014-05-30 13:59:03
【问题描述】:

这是我的代码:

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] [1]:stackoverflow.com/questions/18888059/…

标签: ios json uitableview


【解决方案1】:

我忘记确保作者和标题字符串与 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;
}

【讨论】:

    猜你喜欢
    • 2017-04-22
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    相关资源
    最近更新 更多