【发布时间】:2015-11-30 03:07:24
【问题描述】:
所以我使用 AFNetworking 和 pokeapi 来获取 pokemon 列表。我可以让 tableview 加载,标题是 pokemon 的名称。但我希望字幕成为类型。所以我将基数设置为#define pokeApiBaseURL [NSURL URLWithString: @"http://pokeapi.co/"]
这是我的请求的样子。我创建了一个数组,它为我提供了需要解析并获取类型和所有其他 pokemon 信息的 url 的下半部分。但我似乎无法弄清楚如何让 baseURL 和 resource_uri 创建一个新的 url 供我解析。
NSString *string = [NSString stringWithFormat:@"%@api/v1/pokedex/1/", pokeApiBaseURL];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
self.pokemonArray = [responseObject objectForKey:@"pokemon"];
self.pokemonDetailArray = [[responseObject objectForKey:@"pokemon"]valueForKey:@"resource_uri"];
// NSLog(@"\n%@",detailString);
[self.tableView reloadData];
[self.refreshControl endRefreshing];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Request Failed: %@, %@", error, error.userInfo);
}];
[operation start];
这是我将其添加到单元格的位置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdenifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdenifier forIndexPath:indexPath];
NSDictionary *pokemonDictionary = [self.pokemonArray objectAtIndex:indexPath.row];
NSDictionary *pokemonDetailDictionary = [self.pokemonDetailArray objectAtIndex:indexPath.row];
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
[cell.textLabel sizeToFit];
cell.textLabel.text = [pokemonDictionary objectForKey:@"name"];
cell.detailTextLabel.text = @"PLACEHOLDER"; //WANT THE TYPE TO GO HERE!
return cell;
}
【问题讨论】:
标签: arrays json dictionary afnetworking