나는 코딩이 매우 쉬우 며 현재 막혀있다. 나는 실제 해결책을 찾을 수 없으므로 여기에 내 문제가있다. 여기 누군가가 나를 도울 수 있기를 바란다.Tableview를 거리별로 정렬하면, 스위프트
내가 얻으려고하는 것은 거리를 기준으로 가장 가까운 레스토랑에 데이터를 정렬하는 것입니다. 내가 거리를 계산하고 내 목록보기에 표시 할 수 있지만 불행히도 나는 거리별로 목록을 정렬하는 방법을 알아낼 수 없었습니다. 나는 그것이 거리를 계산하고 구성된 셀을 반환하기 때문이라고 가정하고 있습니다. (이름, 시간 등으로 식당을 정렬 할 수있었습니다.)
내 viewdidload에서 firebase의 내 레스토랑 데이터를 가져 와서 배열에 추가하고 내 tableview를 다시로드합니다.
아래 코드는 거리를 계산하는 코드입니다. 도움이 되었으면 더 많은 정보 또는 코드를 게시해야하는지 알려주세요.
닉
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 }