1

我一直在尝试几天,并在网上搜索试图解决我的问题,但我找不到任何相关信息。

我正在尝试使用 CustomCell 计算从 userLocation 到 UITableView 中我的注释的距离。

我能够在 NSLOG 中获得计算出的距离,但在我的 tableView 中,我的所有单元格都获得了 0.0 公里。

谁能看到我在这里做错了什么?

    - (void)viewDidLoad
{
    [super viewDidLoad];

    self.mapView.delegate = self;
    [self startLocationServices];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"points" ofType:@"plist"];
    NSArray *anns = [NSArray arrayWithContentsOfFile:path];

    for(NSMutableDictionary *note in anns) {
        double doubleLatitude = [[note objectForKey:@"sculptureLatitudeKey"] doubleValue];
        double doubleLongitude = [[note objectForKey:@"sculptureLongitudeKey"] doubleValue];
        Annotation* myAnnotation = [[Annotation alloc] init];
        CLLocationCoordinate2D theCoordinate;
        theCoordinate.latitude = doubleLatitude;
        theCoordinate.longitude = doubleLongitude;
        myAnnotation.coordinate = theCoordinate;
        myAnnotation.title = [note objectForKey:@"sculptureNameKey"];
        myAnnotation.subtitle = [note objectForKey:@"sculptureAddressKey"];
        myAnnotation.sculptureIdKey = [note objectForKey:@"sculptureIdKey"];
        [self.mapView addAnnotation:myAnnotation];
    }
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 5000, 5000);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}

- (void)startLocationServices
{
    if (self.locationManager == nil)
    {
        self.locationManager = [CLLocationManager new];
    }
    [self.locationManager setDelegate:self];
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [self.locationManager setDistanceFilter:kCLDistanceFilterNone];

    [self.locationManager startUpdatingLocation];
}

- (void)stopLocationServices
{
    [self.locationManager stopUpdatingLocation];
    [self.locationManager setDelegate:nil];
}


- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{    
    for (Annotation *annotation in self.mapView.annotations)
    {
        CLLocationCoordinate2D coord = [annotation coordinate];
        CLLocation *annLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];

        CLLocationDistance calculatedDistance = [annLocation distanceFromLocation:newLocation];
        NSLog(@"Nice distance:%.1f m\n", calculatedDistance);
    }
}

我的 NSLOG 显示了这一点,而我的 .plist 中只有 3 个注释

2013-02-13 16:46:14.736 TalkingSculpture[91833:c07] Nice distance:762.1 m
2013-02-13 16:46:14.736 TalkingSculpture[91833:c07] Nice distance:98.8 m
2013-02-13 16:46:14.736 TalkingSculpture[91833:c07] Nice distance:2704.3 m
2013-02-13 16:46:14.737 TalkingSculpture[91833:c07] Nice distance:0.0 m

我的 customCell.h

#import <UIKit/UIKit.h>

@interface customCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *customTitle;
@property (weak, nonatomic) IBOutlet UILabel *customSubTitle;
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;

@end

和 customCell.m

#import "customCell.h"

@implementation customCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

在表格视图中,我填充了 cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *CellIdentifier = @"Cell";
    customCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (!cell)
    {
        cell = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.customTitle.text = [[self.locationsArray objectAtIndex:indexPath.row] valueForKey:@"sculptureNameKey"];
    cell.customSubTitle.text = [[self.locationsArray objectAtIndex:indexPath.row] valueForKey:@"sculptureAddressKey"];
    cell.distanceLabel.text = [NSString stringWithFormat:@"%.1f km\n", _calulatedDistance];

    return cell;
}
4

1 回答 1

0

不知道你是否还需要这个问题的答案。但是每次显示表格视图时,您都需要填充一个数组(或替代)来保存所有当前距离。然后在 cellForRowAtIndexPath 中将标签设置为与该数组的适当距离。您在此代码中所做的是将它设置为 ivar,当您调用它时,它是 nil 或 0。

于 2013-03-27T20:42:22.277 回答