【问题标题】:empty UITableView while parsing JSON with AFNetworking使用 AFNetworking 解析 JSON 时为空 UITableView
【发布时间】:2013-01-21 03:30:34
【问题描述】:

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

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

请看我的代码:

这是Google Drive上的项目

#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;
}

【问题讨论】:

    标签: ios json parsing uitableview afnetworking


    【解决方案1】:

    你需要 [self.tableview reloadData];这将解决问题..

    【讨论】:

      【解决方案2】:

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

      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);
      }];
      

      【讨论】:

      • 哈哈,非常感谢。我花了最后几个小时试图找出问题:S
      猜你喜欢
      • 2013-01-20
      • 2013-06-24
      • 2023-03-10
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多