1

私は何日も試みており、問題を解決するために Web を検索していますが、関連する情報が見つかりません。

CustomCell を使用して、userLocation から UITableView の注釈までの距離を計算しようとしています。

NSLOG で計算された距離を取得できますが、tableView ではすべてのセルで 0.0km を取得しています。

ここで何が間違っているのか誰にもわかりますか?

    - (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 で、ラベルをその配列からの適切な距離に設定します。このコードで行っているのは、呼び出し時に nil または 0 のいずれかである ivar に設定することです。

于 2013-03-27T20:42:22.277 に答える