首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用AFNetworking解析JSON时UITableView为空

使用AFNetworking解析JSON时UITableView为空
EN

Stack Overflow用户
提问于 2013-02-06 10:16:48
回答 2查看 1.5K关注 0票数 2

我正在使用AFNetworking在UITableView上解析youtube JSON数据。

由于某种原因,我得到了一个空的UITable。我不确定我做错了什么。

请看我的代码:

这是Google Drive上的项目

代码语言:javascript
运行
复制
#import "KKViewController.h"

#import "AFNetworking.h"

#import "AFJSONRequestOperation.h"

@interface KKViewController ()

@end

@implementation KKViewController



- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *urlAsString = @"http://gdata.youtube.com/feeds/api/playlists/PL0l3xlkh7UnvLdr0Zz3XZZuP2tENy_qaP?v=2&alt=jsonc&max-results=50";

    NSURL *url = [NSURL URLWithString:urlAsString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];


   AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {



       self.videoMetaData = [JSON valueForKeyPath:@"data.items.video"];

      // NSLog(@" video Meta Data %@", self.videoMetaData);





    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    [operation start];


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{



  return [self.videoMetaData count];

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }


    NSDictionary *titles = [self.videoMetaData objectAtIndex:indexPath.row];

    NSLog(@"%@",titles);

    cell.textLabel.text = [titles objectForKey:@"title"];

    return cell;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-06 10:23:44

您是否尝试过在数据加载后重新加载表视图?

代码语言:javascript
运行
复制
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"JSON = %@", JSON);
    self.videoMetaData = [JSON valueForKeyPath:@"data.items.video"];
    NSLog(@" video Meta Data %@", self.videoMetaData);
    [self.tableView reloadData]; // Now I need to reload the table view data.
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
票数 3
EN

Stack Overflow用户

发布于 2013-02-06 10:31:35

您需要对reloadData执行self.tableview操作;这将解决问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14720515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档