UISegment根据当前位置和之间的距离对表格视图进行排序

时间:2013-04-08 08:09:08

标签: iphone ios uitableview cllocation

我有一个在桌面视图上显示事件的应用。我有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,表格视图应根据距离进行排序

2 个答案:

答案 0 :(得分:1)

使用以下步骤成功实施:

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

2)制作方法:计算用户当前位置和距离之间的距离。活动场地   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);

答案 1 :(得分: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];
}