私はかなり新しくコーディングしていますが、現在は固執していますが、実際の解決策を見つけることができていないので、ここに問題があり、ここの誰かが私を助けることができます。Tableviewを距離でソートすると、Swift
私が達成しようとしているのは、私のデータを最寄りのレストランまでの距離で並べ替えることです。私は、距離を計算し、それを私のリストビューに表示することができますが、残念ながら私はどのように距離でリストをソートするのか分かりませんでした。私はそれが私が距離を計算し、設定されたセルを返すことによると仮定しています。 (名前、時間などでレストランを並べ替えることができました)
私はviewdidload内のfirebaseからレストランデータを取り出し、配列に追加して、テーブルビューをリロードします。
以下は、距離を計算するコードです。 何か助けていただければ幸いです。情報やコードを投稿する必要がある場合はお知らせください。
ニック
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 }