Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-09-2010, 09:45 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 5
ScottyB is on a distinguished road
Default CLLocationManager and UITableView

Hey Everyone,

I've been developing an application for about a month now and am looking for some help accomplishing the following scenario:

The user will select a cell from a starting UITableView which will then load a new UITableView. Once that selection occurs, I need to look up the user's latitude/longitude, hand that information to my server by embedding it in a URL, and then load the returned XML information in that new UITableView.

The problem that I am having is that the CLLocationManager doesn't seem return coordinates instantly enough for the tableview to populate with that data - here is some sample code:

CLController.m file:
Code:
@implementation CLController 

@synthesize locationManager, delegate; 

- (id) init{

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

- (void)locationManager:(CLLocationManager *) manager didUpdateToLocation: (CLLocation *) 
newLocation fromLocation: (CLLocation *) oldLocation{ 

      [self.delegate locationUpdate:newLocation];
} 


- (void)locationManager:(CLLocationManager *) manager didFailWithError: (NSError *) error{
      [self.delegate locationError:error];
} 

- (void) dealloc{
      [self.locationManager release];
      [super dealloc];
}
myTableView.m file, viewDidLoad method:
Code:
- (void)viewDidLoad {

      [super viewDidLoad];
      locationController = [[CLController alloc] init];
      locationController.delegate = self;
      [locationController.locationManager startUpdatingLocation]; 

}
myTableView.m file, locationUpdate method:
Code:
- (void)locationUpdate:(CLLocation *)location{

      CLLocationCoordinate2D loc = [location coordinate];

      lat = [[NSString alloc] initWithFormat:@"%f", loc.latitude];

      lng = [[NSString alloc] initWithFormat:@"%f", loc.longitude];

      NSLog(@"TableView lat= %@", lat);
      NSLog(@"TableView lng= %@", lng);

      [locationController.locationManager stopUpdatingLocation];

      NSMutableString *myURL = [[NSMutableString alloc] initWithString:@"http://mywebsite/somecode.php?lat="]; 


       //NSLog(@"Beginning myTableView XML Parsing"); 

       [myURL appendString: lat];
       [myURL appendString: @"&lng="];
       [myURL appendString: lng];
       [myURL appendString: @"&radius=10"];

      //Here I have various logic to parse the XML and populate an array.
      //In my tableview, numberOfRowsInSection method, it checks this
      //array to return the proper number of cells, which is always returning 0
      //despite the array containing data

      [myTableView reloadData];
      [myURL release];
}
As a side note, I am actually seeing the lat and lng values in my Console, so I know that the method is getting called, but it isn't being called in time for the tableView methods to actually use the data in the method. And [myTableView reloadData] doesn't seem to have any effect.

How can I force myTableView to wait for the locationUpdate before it actually populates the cells? Is there a better way to do all this?
ScottyB is offline   Reply With Quote
Old 08-09-2010, 11:19 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

take your location manager out of your table view class, put it in your app delegate or something.
smithdale87 is offline   Reply With Quote
Old 08-09-2010, 01:07 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 5
ScottyB is on a distinguished road
Default Awesome!

To my amazement, that worked like a charm - I wish I woulda just done that in the first place

Thanks!
ScottyB is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 327
9 members and 318 guests
HemiMG, ilmman, iram91419, linkmx, nadav@webtview.com, Objective Zero, Paul Slocum, stanny, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,656
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, iram91419
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:39 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0