我正在尝试加载我的 UITableView 与当前位置的距离。我正在采取一些小步骤来尝试将纬度加载到 UITableView 中。我的 NSLog 读取的是正确的纬度/经度,但我的表读取的是 0.000。在这一点上,我不确定是我的内存管理还是其他原因。请帮忙。
我的 ViewController.h
#import <UIKit/UIKit.h>
#import "CoreLocation/CoreLocation.h"
@interface TulsaMasterViewController : UITableViewController <CLLocationManagerDelegate>
{
NSArray *_barInfo;
CLLocationManager *lm;
NSString *currentLat;
}
@property (nonatomic, strong) NSArray *barInfo;
@property (nonatomic, strong) NSString *currentLat;
@end
我的 ViewController.m
#import "TulsaMasterViewController.h"
#import "TulsaDetailViewController.h"
#import "Bars.h"
#import "BarDatabase.h"
@implementation TulsaMasterViewController
@synthesize barInfo = _barInfo;
@synthesize currentLat = _currentLat;
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%f", newLocation.coordinate.latitude);
NSLog(@"%f", newLocation.coordinate.longitude);
currentLat = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSString *msg = [[NSString alloc]initWithString:@"Error obtaining location"];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:msg delegate:nil cancelButtonTitle:@"Done" otherButtonTitles:nil];
[alert show];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.barInfo count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//Get the object from the array.
Bars *barObj = [self.barInfo objectAtIndex:indexPath.row];
//Set the coffename.
cell.textLabel.text = barObj.barName;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%f", currentLat];
// Set up the cell
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
TulsaDetailViewController *detailViewController = [segue destinationViewController];
detailViewController.detailItem = [self.barInfo objectAtIndex:[self.tableView indexPathForSelectedRow].row];
}
}
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.barInfo = [BarDatabase database].barInfo;
lm = [[CLLocationManager alloc] init];
lm.delegate = self;
[lm startUpdatingLocation];
}
@end