将geolocation coords传递给UITableViewCell

时间:2015-06-18 17:01:46

标签: ios objective-c uitableview geocoding

首先我用我需要的地址制作了一个NSString

NSString *bldgAddress = [NSString stringWithFormat:@"%@ %@ %@", listingsDisplayed.ADDRESS1, listingsDisplayed.TOWN, listingsDisplayed.ZIPCODE];

然后我打电话给LMGeocoder Github Link

[[LMGeocoder sharedInstance] geocodeAddressString:bldgAddress service:kLMGeocoderGoogleService completionHandler:^(LMAddress *address , NSError *error) {
 if (address && !error) {
 _latitude = [NSString stringWithFormat:@"%f", address.coordinate.latitude];
 _longitude = [NSString stringWithFormat:@"%f", address.coordinate.longitude];
 } else {
 NSLog(@"%@", error);
 }}];

它在这里工作正常,_lat和_long在我的.h文件中声明,值分别指定

cell.lat.text = [NSString stringWithFormat:@"%@,%@", _latitude, _longitude];

问题在我尝试将数据加载到单元格时开始,它显示(null),(null)。 我不明白为什么价值观会消失,任何帮助都会非常感激

编辑: 感谢@dhanush

的代码
    __block UILabel *currentLAbel = cell.lat;
[[LMGeocoder sharedInstance] geocodeAddressString:bldgAddress service:kLMGeocoderGoogleService completionHandler:^(LMAddress *address , NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^{

        if (address && !error) {
            listingsDisplayed.Latitude = [NSString stringWithFormat:@"%f", address.coordinate.latitude];
            listingsDisplayed.Longitude = [NSString stringWithFormat:@"%f", address.coordinate.longitude];

            currentLAbel.text = [NSString stringWithFormat:@"%@,%@", listingsDisplayed.Latitude, listingsDisplayed.Longitude];
            cell.lat = currentLAbel;

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

2 个答案:

答案 0 :(得分:1)

试试这个

    UILabel *currentLAbel = cell.lat;
    [[LMGeocoder sharedInstance] geocodeAddressString:bldgAddress service:kLMGeocoderGoogleService completionHandler:^(LMAddress *address , NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^{

    if (address && !error) {
        listingsDisplayed.Latitude = [NSString stringWithFormat:@"%f", address.coordinate.latitude];
        listingsDisplayed.Longitude = [NSString stringWithFormat:@"%f", address.coordinate.longitude];

        currentLAbel = [NSString stringWithFormat:@"%@,%@", listingsDisplayed.Latitude, listingsDisplayed.Longitude];
        cell.lat = currentLabel;

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

您应该始终在主线程中进行UI更改。最初这些值为null。获得这些值后,您可以在块内设置它们。如果您滚动表格,CurrentLabel将帮助获取特定标签。尝试使用cell.lat。如果它不起作用,请使用当前标签。

答案 1 :(得分:0)

你得到的是lat&块中的长...这是异步的,因此不会在主线程上执行。

你设置了lat&从块中获取其值之前的long值。这就是你获得空值的原因。

你设置断点并跟踪执行流程,你会明白这一点。

祝你好运! :)