Votre code:
NSString *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%f,%f&sensor=false",startPoint,midannotation.coordinate.latitude,midannotation.coordinate.longitude];
NSURL *googleRequestURL=[NSURL URLWithString:url];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
NSString *someString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
// NSLog(@"data:%@",someString);
NSError* error;
NSMutableDictionary* parsedJson = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSArray *allkeys = [parsedJson allKeys];
for(int i = 0; i < allkeys.count; i++){
if([[allkeys objectAtIndex:i] isEqualToString:@"routes"]){
arr = [parsedJson objectForKey:@"routes"];
dic = [arr objectAtIndex:0];
// NSLog(@"ALL KEYS FROM ROUTE: %@", [dic allKeys]);
legs = [dic objectForKey:@"legs"];
// NSLog(@"legs array count %d", legs.count);
for(int i = 0; i < legs.count; i++){
stepsArr = [[legs objectAtIndex:i] objectForKey:@"steps"];
for (int i = 0; i < stepsArr.count; i++) {
NSLog(@"HTML INSTRUCTION %@", [[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"]);
NSLog(@"############################");
NSMutableArray *detail=[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ;
}
}
}
}
});
d'abord vous allez à votre fichier .h ->
@property (nonatomic, strong) NSMutableArray * detailsArray;
Dans.fichier m ->
@synthesize detailsArray;
remplacer ce code
NSMutableArray *detail=[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ;
par cette ->
self.detailsArray = [[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"];
# using table datasource methods
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.detailsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.titleLabel.text = [self.detailsArray objectAtIndex: indexPath.row ];
return cell;
}
}
// Remarque S'il vous plaît définir la méthode de délégué table, si vous écrivez le code signifie par programme tableView.delgate = self;
Utilisez ceci:
self.detaisArray = [[NSMutableArray alloc] init];
// Do any additional setup after loading the view, typically from a nib.
NSString *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%f,%f&sensor=false",startPoint,midannotation.coordinate.latitude,midannotation.coordinate.longitude];
NSURL *googleRequestURL=[NSURL URLWithString:url];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
NSString *someString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
// NSLog(@"data:%@",someString);
NSError* error;
NSMutableDictionary* parsedJson = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSArray *allkeys = [parsedJson allKeys];
for(int i = 0; i < allkeys.count; i++){
if([[allkeys objectAtIndex:i] isEqualToString:@"routes"]){
NSArray *arr = [parsedJson objectForKey:@"routes"];
NSDictionary *dic = [arr objectAtIndex:0];
// NSLog(@"ALL KEYS FROM ROUTE: %@", [dic allKeys]);
NSArray *legs = [dic objectForKey:@"legs"];
// NSLog(@"legs array count %d", legs.count);
for(int i = 0; i < legs.count; i++){
NSArray *stepsArr = [[legs objectAtIndex:i] objectForKey:@"steps"];
for (int i = 0; i < stepsArr.count; i++) {
NSLog(@"HTML INSTRUCTION %@", [[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"]);
NSLog(@"############################");
[self.detaisArray addObject:[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ];
if(i == legs.count-1){
self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 400) style:UITableViewStylePlain];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
[self.view addSubview:self.myTableView];
}
}
}
}
}
});
Vous rechargeait la vue de la table après que les données ont été téléchargées? – Wain
pouvez-vous remplir le tableau 'detail'? –
oui j'ai rechargé mais pas de changement. –