iphone - UISegment 根据当前位置和位置之间的距离对 TableView 进行排序

标签 iphone ios uitableview cllocation

我有一个在表格 View 中显示事件的应用程序。我有 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,表格 View 应该根据距离排序

最佳答案

使用以下步骤成功实现:

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

2) Make 方法:计算用户当前位置和事件地点之间的距离 我猜你有 LAT,很想去事件 field

我的寻找距离的代码

-(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);

关于iphone - UISegment 根据当前位置和位置之间的距离对 TableView 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15874212/

相关文章:

iPhone 在呈现模态视图 Controller 时崩溃

iphone - 是否可以从 iPhone/iPad 向传真机发送消息

ios - html 5 输入日期类型是否足以在所有浏览器中选择日期

ios - iOS 移动应用程序性能测试最好用哪个工具

swift - 使用 SDWebimage 下载并缓存 MKMapSnapshotter 图像

iphone - 卡在设备崩溃上吗?

ios - 如果在我标记所有内容不要备份后仍然在 iCloud 上存储少量数据,我的应用程序是否会再次被拒绝?

iphone - 如何将tableView数据保存到NSUserDefaults?

iphone - 自定义 Tableview 单元格出现问题

ios - 尝试重新加载部分时断言失败错误