-4

我必须做一个项目,在该项目中我必须计算从我的位置(由 gps 检测到)到许多位置在 plist 文件中的餐厅的距离,然后在列表视图中显示距离作为餐厅名称的副标题.

我知道CLLocationDistance并且我写了一个函数,它是:-

-(double)distanceBetPoints
{
    CLLocation *user = [[CLLocation alloc] initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude];
    CLLocationDistance distance = [user distanceFromLocation:restaurant.location];
    return distance;
}

这里位置管理器正在确定我的位置,并且restaurant.location是 plist 文件中的位置条目。

我在单元格配置中写了以下内容:-

NSString *d = [NSString stringWithFormat:@"%@",[mapVC.distanceBetPoints]];
cell.detailTextLabel.text = [@"%@",[d]];

mapVC是我的mapVC类对象,其中存在距离计算方法,这两行都给了我预期标识符的错误。

另外,我如何为每家餐厅分别计算并在 tableview 中显示?我必须将值存储在数组中吗?如果是,那么我应该把我的方法放在哪里,我应该在哪里调用它,以便为每家餐厅计算它并存储在一个数组中。

4

1 回答 1

0

嗯....我认为这是您的错误:

NSString *d = [NSString stringWithFormat:@"%@",[mapVC.distanceBetPoints]];
cell.detailTextLabel.text = [@"%@",[d]];

不应该是这样的:

NSString *d = [NSString stringWithFormat: @"%@", mapVC.distanceBetPoints];
cell.detailTextLabel.text = d;

我不确定,这里的答案似乎如此简单和明显,以至于我可能从未见过这种语法,我假设它不正确,我从未见过这么多括号和那种格式!

另外,我如何为每家餐厅分别计算并在 tableview 中显示?我必须将值存储在数组中吗?如果是,那么我应该把我的方法放在哪里,我应该在哪里调用它,以便为每家餐厅计算它并存储在一个数组中。

将其存储到本地数组中,并使用一种方法(可能应该将其放入viewDidLoad)将餐厅填充到数组中。我假设您的 plist 是一个字典数组(每个字典都是一家餐厅)。但如果没有,你的餐厅名单是什么?一个字符串数组还是什么?

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"MyPlistName" ofType:@"plist"];

restaurantArray = [[NSArray alloc] initWithContentsOfFile: plistPath];

于 2013-01-26T06:56:31.333 回答