【问题标题】:Pass CLLocation data to UITableView将 CLLocation 数据传递给 UITableView
【发布时间】:2012-06-13 18:12:46
【问题描述】:

我正在尝试加载我的 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

【问题讨论】:

    标签: ios xcode uitableview ios5 cllocation


    【解决方案1】:

    你需要在调用 didUpdateToLocation 方法时更新你的 tableview

    【讨论】:

    • 我认为应该是 [self.tableView reloadData] 或类似的东西
    • 为什么要更新 TableView?我只需要我的位置一次。似乎我在将我的位置放入我的 TableView 时遗漏了一些东西。当我将 currentLat 拉入 detailTextLabel.text 时,它的 0
    • Sunrize,我以前没想过,但我同意你更新表格。但是,该表似乎仍然没有被锁定在纬度中。我的 NSLog 显示了正确的纬度/经度,但表格永远不会从 0.0000 移动。
    • 每当我对 tableview 做任何事情时,我都会确保类符合 UITableViewDelegate 和 UITableViewDataSource 协议,看起来你还没有这样做。如果您需要这方面的帮助,请查看此处stackoverflow.com/questions/2454567/…。此外,我会尝试在您的 .h 文件中声明 tableview 变量,并建立从变量到 nib 中的 tableview 的 IBOutlet 连接。然后,当您引用 tableview 时,例如在 [tableview reload] 中,它将引用您的 .nib 文件中的 tableview 或您在 storyboard 中看到的视图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2015-09-23
    相关资源
    最近更新 更多