2012-01-06 4 views
0

Я хочу отсортировать ячейки таблицы в ближайшем к пользователю месте. Предположим, у меня 14 магазинов & Я хочу отсортировать его по местоположению. Есть ли какой-нибудь пример для этого?Сортировка TableView по местоположению?

Спасибо!


Да, У меня есть TableView с 14 клетками & я поставил имена 14 магазинов образуют PLIST в этих клетках с контролем навигации к другим видам с реквизитами (от PLIST тоже). 14 магазинов по всей стране & Я хочу отсортировать их по месту в зависимости от моего местоположения в стране (с помощником GPS, конечно) Я хочу, чтобы магазин ближайших магазинов находился в верхней части стола.

Это мой код сортировки, который мне нужно использовать, но я не могу понять, как &, где его интегрировать со столом. Для всех, кто нуждается, что - не стесняйтесь использовать этот код :) .. много людей нуждаются в том, что:

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

} 

Это будет здорово, если бы кто-то может помочь мне с этим .. Большое спасибо.

+0

Вы можете объяснить это ясно? – Emon

ответ

0

ОК, поэтому я получил код, который выполняет работу с ячейками вместе с приведенным ниже кодом сортировки. Надеюсь, это поможет многим разработчикам, которые в этом нуждаются. Спасибо, что постарались помочь :)

- (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; 
} 
+0

при использовании> двойной lat1 = [lat doubleValue]; и> double lon1 = [lon doubleValue]; являются ли эти свойства членов вашим UITableViewClass? – OghmaOsiris

Смежные вопросы