gpt4 book ai didi

ios - 将地理位置坐标传递给 UITableViewCell 的问题

转载 作者:行者123 更新时间:2023-11-29 01:55:28 24 4
gpt4 key购买 nike

首先我用我需要的地址创建了一个 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);
}
});
}];

最佳答案

试试这个

    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 更改。最初这些值为空。获得这些值后,您可以将其设置在 block 内。如果您正在滚动表格,CurrentLabel 将有助于获取特定标签。尝试使用 cell.lat。如果它不起作用,请使用当前标签。

关于ios - 将地理位置坐标传递给 UITableViewCell 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30921484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com