gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:15:41 25 4
gpt4 key购买 nike

我有一个在表格 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com