0
- (void)viewDidLoad {
detailView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 330, 380)       style:UITableViewStylePlain];
detailView.rowHeight = 55.0f;
detailView.dataSource =self;
detailView.delegate =self;
NSLog(@"finish0");
[self.view addSubview:detailView];
self.title = NSLocalizedString(@"Routes", nil);
self.detailArray = [[NSMutableArray alloc] init];
url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%f,%f&sensor=false",start1,center.latitude,center.longitude];
    NSURL *googleRequestURL=[NSURL URLWithString:url];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
        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];
                legs = [dic objectForKey:@"legs"];
                for(int i = 0;  i < legs.count; i++){
                    NSArray *stepsArr = [[legs objectAtIndex:i] objectForKey:@"steps"];
                    for (int i = 0; i < stepsArr.count; i++) {
                        [self.detailArray addObject:[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ];
                        string = [self.detailArray componentsJoinedByString:@","];
                         NSLog(@"string%@",string);
                      [self stringByStrippingHTML];
                        self.detail=[string componentsSeparatedByString:@","]; 
                        [self.detail retain];
                        [detailView reloadData];                       
                    }}}}        
    });    
 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
    return [detail count];
    NSLog(@"table2");
}   
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        NSLog(@"table4");
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5.0f, 0.0f, 300.0f, 60.0f)];
    label.text=[NSString stringWithFormat:@"%@",[detail objectAtIndex:indexPath.row]];
    label.numberOfLines = 3;
    label.font = [UIFont fontWithName:@"Helvetica" size:(12.0)];
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.textAlignment = UITextAlignmentLeft;
    [cell.contentView addSubview:label];
    [label release];
    return cell;
}
-(NSString *) stringByStrippingHTML {
NSRange r;
while ((r = [string rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
    string = [string stringByReplacingCharactersInRange:r withString:@""];
return string;
}

在编码中 self.detail(array) 的值在触摸 TableView 中的屏幕后显示。我需要在进入 TableView 时立即显示这些值。如果您知道,请任何人帮助。并告诉我为什么 NSArray 的值仅在触摸或滚动 tableview 后显示。

4

2 回答 2

1

下载完数据并用结果填充数组后,您需要重新加载表格视图。那是

url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%f,%f&sensor=false",start1,center.latitude,center.longitude];
NSURL *googleRequestURL=[NSURL URLWithString:url];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
    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];
            legs = [dic objectForKey:@"legs"];
            for(int i = 0;  i < legs.count; i++){
                NSArray *stepsArr = [[legs objectAtIndex:i] objectForKey:@"steps"];
                for (int i = 0; i < stepsArr.count; i++) {
                    [self.detailArray addObject:[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ];
                    string = [self.detailArray componentsJoinedByString:@","];
                    NSLog(@"string%@",string);
                    [self stringByStrippingHTML];
                    self.detail=[string componentsSeparatedByString:@","];
                    [self.detail retain];

                }}}}
[detailView reloadData];
}

还要解释为什么滚动 TableView 后表格会加载;当您在表格视图中滚动时,-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath将调用委托方法(以及其他方法)来更新表格视图。

于 2013-05-23T13:02:22.127 回答
1

试试这个

    [self.detailView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

    instead of [detailView reloadData]; 
于 2013-05-23T13:28:34.003 回答