gpt4 book ai didi

ios - TableView 中的 NSMutableArray 为空

转载 作者:行者123 更新时间:2023-11-28 23:28:30 25 4
gpt4 key购买 nike

我正在使用以下代码来确定我的登录用户与数据库中所有其他用户之间的距离。我成功地能够将找到的距离添加到 NSMutableArray finalDistances,但是我现在正试图在标签 kmAway 的表格 View 单元格中显示这些距离。我想按照它们在每个单元格中返回的相同顺序显示它们。

就是说,当我尝试实现它时,如 ViewController.m 中所示,self.finalDistances 在我的 TableView 中返回为 null,即使它填充在 viewDidLoad 中也是如此。我怎样才能做到这一点?

ViewController.h

@property (nonatomic) CLLocationDistance kilometers;
@property (nonatomic) CLLocation *startLocation;
@property (nonatomic) CLLocation *endLocation;
@property (strong, nonatomic) NSMutableArray *finalDistances;

ViewController.m


NSString *myLocation = self.currentUser[0][@"street_address"];
NSLog(@"MY LOCATION %@", myLocation);

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:myLocation completionHandler:^(NSArray* placemarks2, NSError* error) {
for (CLPlacemark* aPlacemark in placemarks2)
{
for (NSMutableDictionary *multiplelocationsFriend in self.closeByNeighbours) {

NSString *location = [multiplelocationsFriend objectForKey:@"address"];

CLGeocoder *geocoderFriend = [[CLGeocoder alloc] init];
[geocoderFriend geocodeAddressString:location completionHandler:^(NSArray* placemarks, NSError* error) {
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
self.endLocation = [[CLLocation alloc] initWithLatitude:placemark.location.coordinate.latitude longitude:placemark.location.coordinate.longitude];

NSString *myLocation = self.currentUser[0][@"street_address"];
NSLog(@"MY LOCATION %@", myLocation);

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:myLocation completionHandler:^(NSArray* placemarks2, NSError* error) {
for (CLPlacemark* aPlacemark in placemarks2)
{
self.startLocation = [[CLLocation alloc] initWithLatitude:aPlacemark.location.coordinate.latitude longitude:aPlacemark.location.coordinate.longitude] ;
self.kilometers = [self.startLocation distanceFromLocation:self.endLocation] / 1000;
self.finalDistances = [NSMutableArray new];
[self.finalDistances addObject:[NSNumber numberWithDouble:self.kilometers]];
[self.neighboursView reloadData];
}
}];
}
}];
}
}
}];

dispatch_async(dispatch_get_main_queue(), ^{
[self.neighboursView reloadData];
});

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure: %@", [error localizedDescription]);
}];


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *NetworkTableIdentifier = @"sidebarCell";

self.sidetableView.separatorStyle = UITableViewCellSeparatorStyleNone;

sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"sidebarCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}


if (tableView == self.neighboursView)
{
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:@"sidebarCell"];

NSDictionary *userName = [self.closeByNeighbours objectAtIndex:indexPath.row];

NSString *first = [userName objectForKey:@"first name"];
NSString *last = [userName objectForKey:@"last name"];
NSString *area = [userName objectForKey:@"neighbourhood"];
NSString *city = [userName objectForKey:@"city"];

NSDictionary *distances = [self.finalDistances objectAtIndex:indexPath.row];

[[cell kmAway] setText:[NSString stringWithFormat:@"%@", distances]];

[[cell areaLabel] setText:[NSString stringWithFormat:@"%@, %@", area, city]];

NSDictionary *userBio = [self.closeByNeighbours objectAtIndex:indexPath.row];

[[cell username] setText:[NSString stringWithFormat:@"%@ %@", first, last]];


NSString *profilePath = [[self.closeByNeighbours objectAtIndex:indexPath.row] objectForKey:@"photo_path"];

[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];


return cell;
}

...


最佳答案

self.finalDistancesCLGeocoder 上的完成处理程序运行之前不会设置。此外,每次通过完成处理程序中的 for 循环时,它都会重置为一个空的 NSMutableArray

但是,也可能存在其他错误。

关于ios - TableView 中的 NSMutableArray 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57719096/

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