在TableView人口之前获取CoreLocation更新?

时间:2010-06-07 10:48:19

标签: iphone objective-c uitableview core-location

我在uitableview控制器中有重置位置。我实际上想要从两个位置获得距离并在tableview单元格中打印该距离。

问题是,在所有重置位置发生之前,tableview已填满。我怎样才能使得在表填充之前进行所有更新?

继承我的班级:

//
//  EntriesListViewController.m
//  OEAW_App
//
//  Created by Clemens on 6/6/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "EntriesListViewController.h"
#import "EntryDetailController.h"


@implementation EntriesListViewController

@synthesize locationManager;
@synthesize delegate;

NSMutableDictionary *entries;
NSMutableDictionary *dictionary;

CLLocation *coords;

/*- (id) init {
    self = [super init];
    if (self != nil) {
        self.locationManager = [[[CLLocationManager alloc] init] autorelease];
        self.locationManager.delegate = self;
    }
    return self;
}*/

- (CLLocationManager *)locationManager {

    if (locationManager != nil) {
        return locationManager;
    }

    locationManager = [[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    locationManager.delegate = self;

    return locationManager;
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    //coords.longitude = newLocation.coordinate.longitude;
    //coords.latitude = newLocation.coordinate.latitude;
    coords = newLocation;
    NSLog(@"Location: %@", [newLocation description]);
}

- (void)locationManager:(CLLocationManager *)manager
             didFailWithError:(NSError *)error
{
    NSLog(@"Error: %@", [error description]);
}


- (void)viewDidLoad {

    //[[MyCLController alloc] init];
    //[locationManager startUpdatingLocation];

    [[self locationManager] startUpdatingLocation];

    //---initialize the array--- 
    //entries = [[NSMutableArray alloc] init];

    //---add items--- 
    //NSString *Path = [[NSBundle mainBundle] bundlePath];
    //NSString *DataPath = [Path stringByAppendingPathComponent:@"Memorials.plist"];

    dictionary = [[NSDictionary alloc] initWithContentsOfURL:[NSURL URLWithString: @"http://akm.madison.at/memorials.xml"]];

    /*NSDictionary *dssItem = [dictionary objectForKey:@"1"];
    NSString *text = [dssItem objectForKey:@"text"];
    */

    //entries = [[NSMutableDictionary alloc] init];

    NSLog(@"%@", dictionary);


    //Path get the path to MyTestList.plist
    NSString *path=[[NSBundle mainBundle] pathForResource:@"Memorials" ofType:@"plist"];
    //Next create the dictionary from the contents of the file.
    NSDictionary *dict=[NSDictionary dictionaryWithContentsOfFile:path];

    //now we can use the items in the file.
//  self.name.text = [dict valueForKey:@"Name"] ;
    NSLog(@"%@",[dict valueForKey:@"Name"]);

    //---set the title--- 
    self.navigationItem.title = @"Türkendenkmäler";

    [super viewDidLoad];   
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [dictionary count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    NSArray *keys = [dictionary allKeys];
    id key = [keys objectAtIndex:indexPath.row];
    NSDictionary *tmp = [dictionary objectForKey:key];
    NSString *name = [tmp objectForKey:@"name"];
    cell.textLabel.text = name;
    cell.font = [UIFont fontWithName:@"Helvetica" size:12.0];

    CLLocation *location = [[CLLocation alloc] initWithLatitude:[[tmp valueForKey:@"coords_x"] floatValue] 
                                                                                                        longitude:[[tmp valueForKey:@"coords_y"] floatValue]];

    /*CLLocation *newLoc = [[CLLocation alloc] initWithLatitude:coords.latitude 
                                                                                                        longitude:coords.longitude];*/


    //locationController = [[MyCLController alloc] init];



    int distance = [coords distanceFromLocation:location];

    NSLog(@"%@",distance);
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@m",distance];
    //NSLog(@"%@", [getLocation newLoc]);

    return cell;
}

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    EntryDetailController *detailViewController = 
    [[EntryDetailController alloc]
     initWithNibName:@"EntryDetailController" bundle:nil];

    //detailViewController.entrySelected = [dictionary objectAtIndex:indexPath.row];

    NSArray *keys = [dictionary allKeys];
    id key = [keys objectAtIndex:indexPath.row];
    NSDictionary *tmp = [dictionary objectForKey:key];
    NSString *name = [tmp objectForKey:@"name"];
    detailViewController.entrySelected_name = name;

    NSString *location = [tmp objectForKey:@"location"];
    detailViewController.entrySelected_location = location;

    NSString *type = [tmp objectForKey:@"type"];
    detailViewController.entrySelected_type = type;

    NSString *slug = [tmp objectForKey:@"slug"];
    detailViewController.entrySelected_slug = slug;


    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    [entries release];
    [super dealloc];
}


@end

1 个答案:

答案 0 :(得分:1)

防止表视图更新,直到位置管理器可能之后,但有一种更简单的方法可能对您的用户更有意义。我将使用两步过程完成此任务。

  1. 在cellForRowAtIndexPath方法中,为距离提供一些有意义的值,向用户指示该位置尚未更新。例如,检查您的coords值是否为nil,如果是,则将cell.detailTextLabel.text设置为“?”或“ - ”等。
  2. 在locationManager:didUpdateToLocation:fromLocation中,放置此行代码
  3. [self.tableView reloadData];

    这样,当位置管理器获得更精确的数据时,表格视图单元格中的距离值将继续更新。即使位置管理器永远不会更新,也可以提供有意义的数据,这与您永远不会显示表格视图的方法相反。