1

我有一个在表格视图上显示事件的应用程序。我有 UISegment 按字母顺序对这个表进行排序。现在我想根据从用户位置到事件位置的距离对其进行排序。

我有一个包含事件对象的数组,每个对象都包含事件名称、时间、描述等。

在我的 cellForRowAtIndexPath: 我正在这样做

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:                        (NSIndexPath *)indexPath
    {
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
      }
     // Configure the cell...
     if (tableView == self.searchDisplayController.searchResultsTableView) {
         cell.textLabel.text = [[searchResult objectAtIndex:indexPath.row]name];
     }
    else{ 
          CLLocationCoordinate2D coordinate = [_delegate getLocation];

          CLLocation *userLocation =  [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
          CLLocation *location = [[CLLocation alloc] initWithLatitude:[[self.events objectAtIndex:indexPath.row]latitude] longitude:[[self.events objectAtIndex:indexPath.row]longitude]];
          CLLocationDistance distance = [location distanceFromLocation:userLocation];
          cell.textLabel.text = [[self.events objectAtIndex:indexPath.row]name];
          //self.distances = [[NSString alloc] initWithFormat: @"%f", distance];
          [distances addObject:[NSNumber numberWithFloat:distance/1609.344]];
          NSLog(@"distances %@", distances);
         //NSString *_startTime = (NSString *)[[self.events objectAtIndex:indexPath.row]startTime];
         cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f mi",distance/1609.344];
         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
 }

一旦用户点击 UISegment:

    -(void)changeSegment:(id)sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;

switch ([segmentedControl selectedSegmentIndex]) {
    case 0:
    {
        //NSSortDescriptor *ascSorter = [[NSSortDescriptor alloc ] initWithKey:@"name" ascending:YES];
        NSSortDescriptor *ascSorter = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
        [self.events sortUsingDescriptors:[NSArray arrayWithObject:ascSorter]];
        [self.tableView reloadData];
        break;
    }
    case 1:
    {
        //NSSortDescriptor *ascSorter = [[NSSortDescriptor alloc ] initWithKey:@"name" ascending:YES];
        NSSortDescriptor *descSorter = [[NSSortDescriptor alloc] initWithKey:@"location" ascending:YES];
        //[self.events sortUsingDescriptors:[NSArray arrayWithObject:descSorter]];
        [self.distances sortUsingDescriptors:[NSArray arrayWithObject:descSorter]];
        [self.tableView reloadData];
        break;

    }
        break;
    default:
        break;
  }

 }

我现在需要的是一旦用户选项卡 UISegment,表格视图应该根据距离进行排序

4

2 回答 2

1

使用以下步骤成功实施:

1)首先获取用户当前位置。

2) Make 方法:计算用户当前位置和活动地点之间的距离 i guess you have LAT,Long for the Event Venue

我的代码查找距离

-(void)getdistance{

      arr_distance=[[NSMutableArray alloc]init];

        for(int i=0; i<[arr_VenueName count];i++){

        CLLocationCoordinate2D lc;


        lc.latitude = [[arr_lat objectAtIndex:i]doubleValue];
        lc.longitude= [[arr_lng objectAtIndex:i]doubleValue];

        CLLocation  *loc2=[[CLLocation alloc]initWithLatitude:lc.latitude longitude:lc.longitude];

        currentLocation =APPDELEGATE.userLocation;

        CLLocationDistance dist = [currentLocation distanceFromLocation:loc2];

           double miles=dist/1609;

             [arr_distance addObject:[NSString stringWithFormat:@"%.02lf",miles]];
            //NSLog(@"distance meters=%lf miles=%lf",dist,miles);
        }
        //NSLog(@"Array Distance =%@",arr_distance);
    }

3)现在,您必须按照距离数组升序对事件对象数组进行排序。

我的排序代码:

NSArray *sortedArray = [arr_distance sortedArrayUsingComparator:^(id firstObject, id secondObject) {
            return [((NSString *)firstObject) compare:((NSString *)secondObject) options:NSNumericSearch];
        }];

NSArray *sortedValues = [listOfEvents sortedArrayUsingComparator:^(id firstObject, id secondObject) {

            id obj1 = [arr_distance objectAtIndex:[listOfEvents indexOfObject:firstObject]];
            id obj2 = [arr_distance objectAtIndex:[listOfEvents indexOfObject:secondObject]];

            return [((NSString *)obj1) compare:((NSString *)obj2) options:NSNumericSearch];
        }];

        NSLog(@"%@",sortedArray);
        NSLog(@"%@",sortedValues);
于 2013-04-08T09:58:39.637 回答
0

我将首先按与用户的距离对您的事件数组进行排序,然后重新加载数据。

假设events是一个充满event对象的 MutableArray。
假设你有一个对你的引用MKMapView,它告诉你用户的位置。

static int compareAnnotationsByDistance(id event1, id event2) {
    MKUserLocation * me = [mapView userLocation];

    CLLocationDistance d1 = [event1.location distanceFromLocation:me.location];
    CLLocationDistance d2 = [event2.location distanceFromLocation:me.location];

    if(d1 < d2)
        return NSOrderedAscending;
    else if(d2 > d1)
        return NSOrderedDescending;
    else
        return NSOrderedDescending;
}

static int compareAnnotationsByName(id event1, id event2) {
    // Sort here by name...
}

-(void)changeSegment:(id)sender{
    UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;

    switch ([segmentedControl selectedSegmentIndex]) {
    case 0:
    {
        // events is your NSMutableArray containing events
        [events sortUsingFunction:compareAnnotationsByName context:nil];
    }
    case 1:
    {
        // events is your NSMutableArray containing events
        [events sortUsingFunction:compareAnnotationsByDistance context:nil];
    }
        break;
    default:
        break;
    }
    [tableView reloadData];
}
于 2013-04-08T09:12:22.857 回答