按距离对 Tableview 进行排序,Swift
Sorting Tableview by distance, Swift
我是编码方面的新手,目前陷入困境,我无法找到可行的解决方案,所以这是我的问题,希望这里有人能够帮助我。
我想要实现的是按到最近餐馆的距离对我的数据进行排序。我可以计算距离并将其显示在我的列表视图中,但不幸的是我无法弄清楚如何按距离对列表进行排序。我假设这是由于我计算距离然后返回配置的单元格。 (我已经能够按名称、营业时间等对餐厅进行排序)
我在 viewdidload 中从 firebase 中提取我的餐厅数据,将其添加到一个数组,然后重新加载我的 tableview。
下面是我计算距离的代码。
非常感谢任何帮助,如果我需要 post 更多信息或代码,请告诉我。
尼克
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "restaurantCell", for: indexPath) as? RestaurantCell else { return UITableViewCell() }
cell.selectionStyle = .none
let restaurantInfo = restaurantDataArray[indexPath.row]
let coordinate0 = CLLocation(latitude: RestaurationerVC.lat, longitude: RestaurationerVC.lon)
let coordinate1 = CLLocation(latitude: restaurantInfo.latitude, longitude: restaurantInfo.longitude)
let mulitplier = pow(10.0, 1.0)
let distanceInKm = (coordinate0.distance(from: coordinate1)) / 1000
let distanceRounded = round(distanceInKm * mulitplier) / mulitplier
cell.configureCell(title: restaurantInfo.title!, restaurantImg: restaurantInfo.subtitle!, distance: "\(distanceRounded) Km", hours: restaurantInfo.hours, restaurantType: restaurantInfo.restaurantType, photo: restaurantInfo.photo)
return cell
}
我建议您对从 Firebase 获得的数组进行排序,然后将数据重新加载到 tableView 中。尝试对 tableview 本身进行排序是非常低效的。当您说您在返回可以更改以在您的数据模型中计算它的单元格之前计算距离时。尝试创建一个结构来表示一个单元格的数据(我们称之为结构餐厅)。这可以为所有计算数据提供变量。然后你可以把它们都放在 restaurants = Restaurant 中,你可以实现按距离排序,然后在你的 tableview cellforrowat indexpath 函数中使用这个数组。
我是编码方面的新手,目前陷入困境,我无法找到可行的解决方案,所以这是我的问题,希望这里有人能够帮助我。
我想要实现的是按到最近餐馆的距离对我的数据进行排序。我可以计算距离并将其显示在我的列表视图中,但不幸的是我无法弄清楚如何按距离对列表进行排序。我假设这是由于我计算距离然后返回配置的单元格。 (我已经能够按名称、营业时间等对餐厅进行排序)
我在 viewdidload 中从 firebase 中提取我的餐厅数据,将其添加到一个数组,然后重新加载我的 tableview。
下面是我计算距离的代码。 非常感谢任何帮助,如果我需要 post 更多信息或代码,请告诉我。
尼克
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "restaurantCell", for: indexPath) as? RestaurantCell else { return UITableViewCell() } cell.selectionStyle = .none let restaurantInfo = restaurantDataArray[indexPath.row] let coordinate0 = CLLocation(latitude: RestaurationerVC.lat, longitude: RestaurationerVC.lon) let coordinate1 = CLLocation(latitude: restaurantInfo.latitude, longitude: restaurantInfo.longitude) let mulitplier = pow(10.0, 1.0) let distanceInKm = (coordinate0.distance(from: coordinate1)) / 1000 let distanceRounded = round(distanceInKm * mulitplier) / mulitplier cell.configureCell(title: restaurantInfo.title!, restaurantImg: restaurantInfo.subtitle!, distance: "\(distanceRounded) Km", hours: restaurantInfo.hours, restaurantType: restaurantInfo.restaurantType, photo: restaurantInfo.photo) return cell }
我建议您对从 Firebase 获得的数组进行排序,然后将数据重新加载到 tableView 中。尝试对 tableview 本身进行排序是非常低效的。当您说您在返回可以更改以在您的数据模型中计算它的单元格之前计算距离时。尝试创建一个结构来表示一个单元格的数据(我们称之为结构餐厅)。这可以为所有计算数据提供变量。然后你可以把它们都放在 restaurants = Restaurant 中,你可以实现按距离排序,然后在你的 tableview cellforrowat indexpath 函数中使用这个数组。