-4

我是初学者,我已经阅读了 StackOverflow 上关于我的问题的所有内容 - 我的带有 json 数据的应用程序在 TableViewController 上没有显示任何内容。我可能遗漏了一些明显的东西,但是非常感谢您的帮助。(我正在使用最新的 Xcode 5 DP,如果它很重要的话)。

TableVC.h

@interface TableVC : UITableViewController <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) NSDictionary *kinos;
@property (retain, nonatomic) UITableView *tableView;

-(void)fetchKinos;

@end

TableVC.m文件是

@interface TableVC ()

@end

@implementation TableVC


- (void)viewDidLoad
{
     [self fetchKinos];
    [self.tableView reloadData];
    [super viewDidLoad];
}

-(void)fetchKinos {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.adworldmagazine.com/json.json"]];
        NSError *error;
        _kinos = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    });

}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _kinos.count;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    //[self configureCell:cell atIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSArray *entities = [_kinos objectForKey:@"entities"];
    NSDictionary *kino = [entities objectAtIndex:indexPath.row];
    NSDictionary *title = [kino objectForKey:@"title"];
    NSString *original = [title objectForKey:@"original"];
    NSString *ru = [title objectForKey:@"ru"];
    cell.textLabel.text = original;
    cell.detailTextLabel.text = ru;

    return cell;

}
@end
4

2 回答 2

0

您的 JSON 响应字典包含一个数组,该数组的entities计数需要从表视图方法中返回。

在此处输入图像描述

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.kinos objectForKey:@"entities"] count];
}

此外,还有一个建议,当您声明strong属性时,请尝试访问它们self.propertyName而不是访问 ivar like _propertyName

希望有帮助!

于 2013-08-14T11:58:34.647 回答
-1

I accessed your url- http://www.adworldmagazine.com/json.json on my browser.
The response returns a JSON with root: dictionary.

So,

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _kinos.count;
}

wont work.

Use this instead:

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [_kinos objectForKey:@"entities"].count;
    }
于 2013-08-14T11:59:39.200 回答