将cllocation传递给tableview

时间:2013-04-06 17:15:51

标签: iphone ios uitableview cllocation

我正在尝试显示用户位置和目标位置之间距离的应用。我打算获取用户位置didUpdateToLocation方法并存储在数组中。然后,将它放在表视图委托“cellForRowAtIndexPath”中以计算距离并显示在表格单元格中。但是,它不起作用。请参阅下面的代码。有人可以帮忙吗?

-(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;

}

它崩溃并在控制台的数组中没有告诉我(见下文)。但是,我在'didUpdateToLocation'中NSLog这个数组,它可以正确显示它。

**** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'*

然后,我尝试在cellForRowAtIndexPath中的NSLog locationMeasurements(数组)

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) "
)

有人可以给我一些建议吗?我该怎么办?

提前感谢。

1 个答案:

答案 0 :(得分:0)

它正在崩溃,因为您正在尝试读取不存在的位置测量值。从数组中与数值不对应的位置从数组中获取值是错误的。

通常,如果您使用数据数组来填充UITableView(这是常见的),则表中的行数应与数组的大小相匹配:

-(NSInteger)tableView:(UITableView*)tableView 
            numberOfRowsInSection:(NSInteger)section 
{
    return [self.locationMeasurements count];
}

如果您这样做,每次向测量数组添加CLLocation时,您的表格视图都应使用新行进行更新。

每次添加测量时,您都可以添加新行,而不是重新加载整个表,这很容易,因为您总是追加。它还允许您为添加的行设置动画。替换

[locationMeasurements addObject:currentLocation];
[self.tableView reloadData];

[locationMeasurements addObject:currentLocation];
NSIndexPath *newPath = [NSIndexPath indexPathForRow:([locationMeasurements count] - 1)
                                          inSection:0];
NSArray *paths = [NSArray arrayWithObject:newPath];
[self.tableView insertRowsAtIndexPaths:paths
                      withRowAnimation:UITableViewRowAnimationBottom];