2017-12-04 20 views
0

나는 코딩이 매우 쉬우 며 현재 막혀있다. 나는 실제 해결책을 찾을 수 없으므로 여기에 내 문제가있다. 여기 누군가가 나를 도울 수 있기를 바란다.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 
    } 
    

답변

1

난 당신이 중포 기지에서 얻을 당신의 배열을 정렬하고있는 tableView의 데이터를 다시로드 좋습니다. tableview 자체를 정렬하는 것은 매우 비효율적입니다. yuor 데이터 모델에서 계산하기 위해 변경할 수있는 셀을 반환하기 직전 거리를 계산한다고 말한 경우. 한 셀의 데이터를 나타내는 구조체를 만들어보십시오 (구조체를 구조체라고 부르 자). 계산 된 모든 데이터에 대해 변수가있을 수 있습니다. 그런 다음 식당에 모두 넣을 수 있습니다 = Restaurant 그리고 거리별로 정렬을 구현 한 다음이 배열을 tableview cellforrowat indexpath 함수에서 사용할 수 있습니다.