0

我正在尝试用到当前位置的点和距离填充我的 tableView。

我遇到了初始化属性的问题。

在 .h 文件中:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface SIBViewController : UIViewController
<CLLocationManagerDelegate>
{
    NSArray *_data;
    CLLocationManager *locationManager;
    CLLocation *currentLocation;
}

@property (nonatomic, retain) CLLocation *currentLocation;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

在 .m 文件中:

#import "SIBViewController.h"
#import "atmCell.h"
#import "sibAtmData.h"

@interface SIBViewController ()

@end


@implementation SIBViewController

@synthesize currentLocation;

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self startSignificantChangeUpdates];

    _data = [sibAtmData fetchData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellIdentifier = @"atmCell";

    atmCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    sibAtmData *item = [_data objectAtIndex:indexPath.row];

    cell.titleLabel.text = item.title;
    cell.subtitleLabel.text = item.subtitle;

    CLLocationDistance distance = [self.currentLocation distanceFromLocation:item.location];

    cell.distanceLabel.text = [NSString stringWithFormat:@"%.1f km", distance/1000];

    NSLog(@"distance: %f", distance);

    return cell;

}


- (void)startSignificantChangeUpdates
{
    // Create the location manager if this object does not
    // already have one.
    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;
    [locationManager startMonitoringSignificantLocationChanges];
}

// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    // If it's a relatively recent event, turn off updates to save power
    self.currentLocation = [locations lastObject];
    NSDate* eventDate = currentLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (abs(howRecent) < 15.0) {

        [self.tableView reloadData];

        // If the event is recent, do something with it.
        NSLog(@"latitude %+.6f, longitude %+.6f\n",
              currentLocation.coordinate.latitude,
              currentLocation.coordinate.longitude);
    }


}

@end

但是currentLocation是空的:

currentLocation CLLocation *    0x00000000

我试着写viewDidLoad

currentLocation = [[CLLocation alloc] init];

但这对我没有帮助。

内存分配给对象,但对象创建时没有 _latitude 和 _longitude 属性我做错了什么?

4

0 回答 0