我正在嘗試顯示用戶位置和目標位置之間的距離的應用程序。我打算獲取用戶位置didUpdateToLocation方法並存儲在數組中。然後,把它放在表格視圖委託「cellForRowAtIndexPath」來計算距離並顯示在表格單元格中。但是,它不起作用。請參閱下面的代碼。任何人都可以幫忙嗎?通過cllocation到tableview
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
if (newLocation.horizontalAccuracy < 0) return;
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;
if (currentLocation == nil || currentLocation.horizontalAccuracy > newLocation.horizontalAccuracy) {
self.currentLocation = newLocation;
// locationMeasurements is NSMutableArray to store currentLocation (CLLocation) information.
[locationMeasurements addObject:currentLocation];
[self.tableView reloadData];
}
}
}
- (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] autorelease];
}
CLLocation *location = [locationMeasurements objectAtIndex:indexPath.row];
return cell;
}
它崩潰並告訴我在控制檯的數組中沒有任何東西(見下文)。但是,我NSLog這個數組在'didUpdateToLocation',它可以正確顯示它。
**** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'*
然後,我嘗試的NSLog locationMeasurements(陣列)中的cellForRowAtIndexPath
NSLog(@"locationMeasurements ===%@",[locationMeasurements description]);
它顯示在幾條語句空白信息,然後在以後顯示正確的信息。見下文。
locationMeasurements ===(
)
locationMeasurements ===(
)
locationMeasurements ===(
)
locationMeasurements ===(
)
locationMeasurements ===(
"<+37.33233141,-122.03121860> +/- 5.00m (speed 0.00 mps/course -1.00) "
)
locationMeasurements ===(
"<+37.33233141,-122.03121860> +/- 5.00m (speed 0.00 mps/course -1.00) "
)
locationMeasurements ===(
"<+37.33233141,-122.03121860> +/- 5.00m (speed 0.00 mps/course -1.00) "
)
locationMeasurements ===(
"<+37.33233141,-122.03121860> +/- 5.00m (speed 0.00 mps/course -1.00) "
)
有人可以給我一些建議嗎?我該怎麼辦?
提前致謝。
我會盡力。感謝您的幫助。 – 2013-04-11 15:49:55