您的代码:
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"] ;
}
}
}
}
});
首先你去你的 .h 文件 -->
@property (nonatomic, strong) NSMutableArray * detailsArray;
在 .m 文件中 -->
@synthesize detailsArray;
替换此代码
NSMutableArray *detail=[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ;
通过这个 -->
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;
}
}
//注意请设置table的委托方法,如果是编程方式写代码意味着 tableView.delgate = self;
用这个 :
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];
}
}
}
}
}
});