2012-01-11 11 views
5

tôi nhận được khoảng cách của 2 điểm như thế nàythứ tự các tế bào tableview bởi phụ đề

[userLocation distanceFromLocation: annotationLocation]/1000; 

và thiết lập này để phụ đề của một tableview như hình dưới đây screenshot http://i41.tinypic.com/ruxv74.png

câu hỏi là , tôi có thể đặt bàn này bằng khoảng cách (phụ đề) không?

Cảm ơn!

và xin lỗi vì tiếng anh xấu = x

+0

Douglas, bạn đã sử dụng giải pháp nào nhất? Bạn đang sử dụng CoreData? – marciokoko

+0

giải pháp ferostar hoạt động cho tôi. Tôi đang sử dụng tập tin .plist. phân bổ và chú thích Vị trí đều là CLLocation. Vì vậy, tôi sử dụng -initWithLatitude: kinh độ: và -distanceFromLocation: để có được khoảng cách và sắp xếp nó trên một NSArray. –

+0

Có lẽ nó sẽ là tốt nhất để trò chuyện nhưng tôi có một loạt các địa điểm với lat và dài. Tôi tính khoảng cách từ userLocation đến từng vị trí. Vì vậy, tôi có tất cả các dữ liệu, vì vậy những gì tôi đặt trong mảng mới? Tại đây: http: //stackoverflow.com/questions/14845434/how-do-i-sort-table-cells-from-core-data – marciokoko

Trả lời

6

Bạn có thể đặt các tế bào của UITableView bạn cách nào để muốn, nhưng bạn phải làm điều đó trước khi hiển thị chúng, khi bạn tạo nguồn dữ liệu của bảng. Nếu bạn sử dụng NSFetchResultsController, bạn có thể đặt khoảng cách làm bộ mô tả sắp xếp. Và nếu bạn đang sử dụng một NS đơn giản (Mutable) Array, hãy sắp xếp nó trước khi biến nó trở thành nguồn của bảng.

Giống như Chris nói, bạn có thể làm điều đó với

NSSortDescriptor *titleSorter= [[NSSortDescriptor alloc] initWithKey:@"annotationLocation" ascending:YES]; 

Nếu nó là một NSArray những gì bạn đang sử dụng, sau đó:

[arrayOfObjects sortUsingDescriptors:[NSArray arrayWithObject:titleSorter]; 

và nếu nó là một NSFetchResultsController:

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:titleSorter, nil]; 
[fetchRequest setSortDescriptors:sortDescriptors]; 
+0

Hoàn hảo! nó hoạt động rất tốt cho tôi. Cảm ơn mọi người =] –

+0

nhưng chú thíchĐịa điểm không có trong cơ sở dữ liệu. Tôi có lat và dài. – marciokoko

0

Tạo một mảng với khoảng cách này, đặt hàng theo cách bạn muốn và

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *cellIdentifier = @"cellIdentifier"; 
    UITableViewCell *_cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (_cell == nil) { 
     _cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease]; 
    } 
    _cell.textLabel.text = @"Transcripts"; 
    _cell.detailTextLabel.text = [yourArray objectAtIndex:indexPath.row]; 
    return _cell; 
} 

tốt, một cái gì đó như thế sẽ thực hiện thủ thuật.

Hy vọng nó giúp

+0

Trong cơ sở dữ liệu CD của tôi, tôi chỉ có vĩ độ và kinh độ.Hiện tại, các vị trí được sắp xếp theo ngày như sau: ' [yêu cầu setSortDescriptors: [NSArray arrayWithObject: [NSSortDescriptor sortDescriptorWithKey: @" date "ascending: YES]]]; self.dates = [self.managedObjectContext executeFetchRequest: lỗi yêu cầu: & lỗi]; ' – marciokoko

1

Tạo một NSSortDescriptor để sắp xếp hàng của bạn:

NSSortDescriptor *titleSorter= [[NSSortDescriptor alloc] initWithKey:@"annotationLocation" ascending:YES]; 
[arrayOfObjects sortUsingDescriptors:[NSArray arrayWithObject:titleSorter]; 
0

Giả sử bạn có một số đối tượng nhất định tổ chức một phối hợp và đưa các đối tượng vào một địa điểm mảng, bạn có thể sử dụng một so sánh khối bằng cách thực hiện:

locations = [locations sortedArrayUsingComparator: ^(id a, id b) { 

    CLLocationDistance dist_a= [[a objectsForKey:@"coordinate"] distanceFromLocation: userPosition]; 
    CLLocationDistance dist_b= [[b objectsForKey:@"coordinate"] distanceFromLocation: userPosition]; 
    if (dist_a < dist_b) { 
     return (NSComparisonResult)NSOrderedAscending; 
    } else if (dist_a > dist_b) { 
     return (NSComparisonResult)NSOrderedDescending; 
    } else { 
     return (NSComparisonResult)NSOrderedSame; 
    } 
} 

bạn thêm mã này vào viewWillAppear: của bạn để có được địa điểm được cập nhật mỗi khi bạn hiển thị tableView .