1

我已经编写了一些代码来从作为“reverseGeocodeLocation”方法的一部分的完成块处理程序内部修改实例变量。首先是代码:

- (void)reverseGeocodeLocation:(CLLocation *)aLocation {
    CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
    __block __strong NSArray *dataArray;

    [geoCoder reverseGeocodeLocation:aLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error == nil && [placemarks count] > 0) {
            CLPlacemark *placemark = [placemarks lastObject];
            CLLocation *userLocation = [placemark location];

            NSString *latitude = [NSString stringWithFormat:@"Lat. %f degrees", userLocation.coordinate.latitude];
            NSString *longitude = [NSString stringWithFormat:@"Long. %f degrees", userLocation.coordinate.longitude];
            NSString *altitude = [NSString stringWithFormat:@"Alt. %f m", userLocation.altitude];
            NSString *speed = [NSString stringWithFormat:@"Speed %f m/s", userLocation.speed];
            NSString *course = [NSString stringWithFormat:@"Course %f degrees", userLocation.course];

            dataArray = @[latitude, longitude, altitude, speed, course];

        // The data array is perfectly populated!!

            NSLog(@"Just checking... %@", dataArray);

        dispatch_sync(dispatch_get_main_queue(), ^{
            self.tableViewRows = [NSMutableArray arrayWithArray:dataArray];
            [self.tableView reloadData];
        });

        } else {
            NSLog(@"%@", error.debugDescription);
        }
    }];

}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

    CLLocation *currentLocation = [locations lastObject];
    [self reverseGeocodeLocation:currentLocation];
}

这是 tableView 的代码:

#pragma mark -
#pragma mark TabeView

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.text = [self.tableViewRows objectAtIndex:indexPath.row];
    }

    return cell;
}

其余的 VC:

- (id)init {
    self = [super init];
    if (self) {

        if ([CLLocationManager locationServicesEnabled]) {
            self.locationManager = [[CLLocationManager alloc] init];
            self.locationManager.delegate = self; // location manager delegate
            self.locationManager.distanceFilter = 1000;
            [self.locationManager startUpdatingLocation];
        }

        // Create the Rows Array for the TableView
        self.tableViewRows = [[NSMutableArray alloc] init];
    }
    return self;
}

-(void)viewDidLoad {
    [super viewDidLoad];

    // Create the TableView
    UITableView *table = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStyleGrouped];
    table.dataSource = self;
    table.delegate = self;
    [self.view addSubview:table];
}

嗯...如您所见,我想填充实例变量“tableViewRows”。临时数组“dataArray”的人口工作得很好!所有的值都显示出来了。并且方法内的实例变量的填充也可以正常工作!但仅在完成块内部。

一旦我尝试访问完成块之外的实例变量,我就会收到一条错误消息,即数组内没有值。我错过了什么重要的事情吗?

我已经读过“reverseGeocodeLocation”是一条异步消息,当我尝试读取实例变量时还无法完成。但在我看来不应该这样,因为完成块已经被调用,我可以记录完成块内的数组条目。

我不知道现在该做什么。我有一种感觉,这是一个非常简单的解决方案,我现在几个小时都完全没有得到它。也许你可以帮我一把?

谢谢

4

1 回答 1

0

所以看起来你告诉表视图你总是有 5 行,但它可能不是真的。您应该要求self.tableViewRows.count实际计数。鉴于此,我认为您在第一次加载表视图时崩溃,当它尝试重新加载自身时,但 tableViewRows 是空的。

另外,您应该将您的 tableView 分配给 viewDidLoad 中的属性

于 2013-04-28T07:58:34.113 回答