Sorting 按位置对TableView排序?
我想按离用户最近的位置对tableview单元格进行排序。 假设我有14家商店&我想按位置对其进行排序。 有没有这样做的例子 谢谢 对, 我有一个包含14个单元格的tableview&我将14个存储区的名称从plist放在那些带有导航控件的单元格中,并从plist中获取其他视图的详细信息。 这14家店遍布全国&我想根据我在国内的位置,在全球定位系统的帮助下,按地点对它们进行分类 我希望neares商店在桌子的最上面 这是我需要使用的排序代码,但我不知道如何以及在何处将其与表集成。 对于所有需要的人,请随意使用此代码:。。很多人都需要:Sorting 按位置对TableView排序?,sorting,uitableview,location,Sorting,Uitableview,Location,我想按离用户最近的位置对tableview单元格进行排序。 假设我有14家商店&我想按位置对其进行排序。 有没有这样做的例子 谢谢 对, 我有一个包含14个单元格的tableview&我将14个存储区的名称从plist放在那些带有导航控件的单元格中,并从plist中获取其他视图的详细信息。 这14家店遍布全国&我想根据我在国内的位置,在全球定位系统的帮助下,按地点对它们进行分类 我希望neares商店在桌子的最上面 这是我需要使用的排序代码,但我不知道如何以及在何处将其与表集成。 对于所有需要的
- (NSComparisonResult)compareDistance:(id)obj
{
CLLocation* loc = [[CLLocation alloc]initWithLatitude:32.066157 longitude:34.777821];
StoreAnnotation* operand1 = self;
StoreAnnotation* operand2 = obj;
CLLocation* operand1Location = [[CLLocation alloc]initWithLatitude:operand1.coordinate.latitude longitude:operand1.coordinate.longitude];
CLLocationDistance distanceOfLocFromOperand1 = [loc distanceFromLocation:operand1Location];
NSLog(@"The distance of loc from op1 = %f meters", distanceOfLocFromOperand1);
CLLocation* operand2Location = [[CLLocation alloc]initWithLatitude:operand2.coordinate.latitude longitude:operand2.coordinate.longitude];
CLLocationDistance distanceOfLocFromOperand2 = [loc distanceFromLocation:operand2Location];
NSLog(@"The distance of loc from op2 = %f meters", distanceOfLocFromOperand2);
if (distanceOfLocFromOperand1 < distanceOfLocFromOperand2)
{
return NSOrderedAscending;
}
else if (distanceOfLocFromOperand1 > distanceOfLocFromOperand2)
return NSOrderedDescending;
else return NSOrderedSame;
}
如果有人能帮我,那就太好了。。
非常感谢。好的,我得到了处理单元格的代码以及下面的排序代码。 我希望它能帮助很多需要它的开发者。 感谢您的帮助:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayFromPlist count];
NSLog(@"Array SIZE = %d",[arrayFromPlist count]);
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
StoreAnnotation* tmpSt = [[Storage sharedStorage].sortedStoresArr objectAtIndex:indexPath.row];
cell.textLabel.text = tmpSt.title;
cell.textLabel.textColor = [UIColor whiteColor];
double lat1=[lat doubleValue];
double lon1=[lon doubleValue];
CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lon1];
CLLocation *distForIndex = [[CLLocation alloc] initWithLatitude:tmpSt.coordinate.latitude longitude:tmpSt.coordinate.longitude];
CLLocationDistance distance = [userLocation distanceFromLocation:distForIndex];
NSLog(@"DISTANCE FOR CELL %d - %f",indexPath.row, distance);
cell.detailTextLabel.text = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%0.1f kmh", distance/1000]];
cell.detailTextLabel.textColor = [UIColor yellowColor];
cell.imageView.image = [UIImage imageNamed:@"imageName.png"];
UIView* backgroundView = [[ UIView alloc ] initWithFrame:CGRectZero];
backgroundView.backgroundColor = [ UIColor blackColor ];
cell.backgroundView = backgroundView;
for ( UIView* view in cell.contentView.subviews )
{
view.backgroundColor = [ UIColor blackColor ];
}
return cell;
}
当您使用>双lat1=[lat doubleValue];and>double lon1=[lon doubleValue];这些成员属性是否属于UITableViewClass?