Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios tableView无法以json格式显示从web解析的数据_Ios_Json_Uitableview - Fatal编程技术网

Ios tableView无法以json格式显示从web解析的数据

Ios tableView无法以json格式显示从web解析的数据,ios,json,uitableview,Ios,Json,Uitableview,我使用和服创建了一个api,下面是我的代码 #import "PlayerRankingsTableViewController.h" #import "RankingsTableViewCell.h" #define kBgQueue dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) #define kPlayerRankingsURL [NSURL URLWithString:@"https://www.kimon

我使用和服创建了一个api,下面是我的代码

#import "PlayerRankingsTableViewController.h"
#import "RankingsTableViewCell.h"

#define kBgQueue dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define kPlayerRankingsURL [NSURL URLWithString:@"https://www.kimonolabs.com/api/bg6tcuuq?apikey=xgp4nU6xA9UcBWSe0MIHcBVbAWz5v4wR"]


@interface PlayerRankingsTableViewController () {

}

@property (nonatomic, strong) NSArray *playerRankings;

@end

@implementation PlayerRankingsTableViewController


- (void)viewDidLoad {

[super viewDidLoad];


dispatch_async(kBgQueue, ^{
    NSData *data = [NSData dataWithContentsOfURL:
                    kPlayerRankingsURL];
    [self performSelectorOnMainThread: @selector(initializePlayerRankingsArray:)
                           withObject:data waitUntilDone:YES];
});

}

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

- (NSArray *)initializePlayerRankingsArray:(NSData *)responseData {
NSError* error;
NSDictionary *json = [NSJSONSerialization
                      JSONObjectWithData:responseData
                      options:kNilOptions
                      error:&error];

NSArray *myPlayerRankings = [[json objectForKey:@"results"]objectForKey:@"collection1"];
self.playerRankings = myPlayerRankings;
NSLog(@"%@", self.playerRankings);

return self.playerRankings;

}

#pragma mark - Table view data source

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

return 1;
}

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

return [self.playerRankings count];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RankingsTableViewCell *cell = (RankingsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];

if (cell == nil) {
    cell = (RankingsTableViewCell *)[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}

NSDictionary *rankings = [self.playerRankings objectAtIndex: indexPath.row];

NSString *rank = [rankings objectForKey:@"rank"];
NSString *name = [rankings objectForKey:@"name"];
NSString *points = [rankings objectForKey:@"points"];


[cell.playerRank setText:rank];
cell.playerName.text = name;
cell.playerPoints.text = points;


return cell;
}

@end
我认为数据解析过程没有问题,因为控制台正确地显示了从web解析的数据。 然而,当我运行应用程序时,我只看到一张空表。 同样,我认为这对编程来说可能是一件简单而新鲜的事情


提前感谢您,很抱歉给您带来这么大的负担。

initializePlayerRankingsArray:
中,您不应该返回数组,因为那里没有任何东西可以接收它。您已经设置了
self.playerrorings
,这样就足够了


此方法缺少的是
[self.tableView reloadData]
(这应该是设置了
self.playerrangings
之后的最后一行)。

我确信您有一个带有
reuseIdentifier:@“cell”
的单元格,其中包含三个标签,您正在使用这些标签设置排名名称和积分。您还可以在不同的位置使用
NSLog
,以确保单元格不是
nill
等。我确信它只是一个占位符,但如果不是,您可能不希望将API密钥发布到公共应用程序forum@WarrenBurton所以我在initializePlayerRankingsArray中添加了重载数据?是的,获取数据后,需要重新加载表格。我添加了这一行,当我运行应用程序时,在json登录到控制台后,我得到了“由于未捕获异常而终止”。@ShawnClarke,错误消息说什么?通常它会给出未捕获异常的原因。@ShawnClark您是否将情节提要中单元格的类更改为RankingsTableViewCell?错误表明您得到的是UITableViewCell,而不是RankingsTableViewCell。