【发布时间】:2013-02-14 16:29:13
【问题描述】:
在我的应用程序中,我获取一个核心数据库并将结果放入一个名为 self.stores 的数组中。我将这些商店位置转换为具有距离属性的 MyLocation 对象。我像这样绘制 MyLocation 对象:
- (void)plotStorePositions:(NSString *)responseString {
for (id<MKAnnotation> annotation in _mapView.annotations) {
[_mapView removeAnnotation:annotation];
}
//CYCLE THRU STORE OBJECTS
for (Store * storeObject in self.stores) {
NSString * latitude = storeObject.latitude;
NSString * longitude = storeObject.longitude;
NSString * storeDescription = storeObject.name;
NSString * address = storeObject.address;
//CREATE MYLOCATION OBJECTS
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
MyLocation *annotation = [[MyLocation alloc] initWithName:storeDescription address:address coordinate:coordinate distance:0];
//CREATE A PIN FOR EACH MYLOCATION ANNOTATION
CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
//SET USERLOCATION (Must move this code out)
self.userLocation = [[CLLocation alloc] initWithLatitude:self._mapView.userLocation.coordinate.latitude longitude:self._mapView.userLocation.coordinate.longitude];
//SET USERLOCATION TO APPDELEGATE (Must move this code out)
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
myDelegate.userLocation = self.userLocation;
//CALCULATE DISTANCE AND SET ITS THAT MYLOCATION's DISTANCE PROPERTY
CLLocationDistance calculatedDistance = [pinLocation distanceFromLocation:userLocation];
annotation.distance = calculatedDistance/1000;
[_mapView addAnnotation:annotation];
}
}
这是在地图视图中。然后我有一个表格视图,我在其中获取相同的核心数据库并再次获取 self.stores。然后我循环遍历 self.stores 数组以创建 STORE storeObjects 以放入我的 tableview 单元格。但我想对这些细胞进行分类。如何获取应该在内存中的 MyLocation 对象?我需要将它们传递给 appDelegate 还是 tableview 控制器?
【问题讨论】:
-
使用正确的标签。你认为对象标签是获取IOS开发答案的合适标签吗?
-
我很难理解你的问题。您是在问如何根据距离对
self.stores对象进行排序? -
我有一个 mapview 控制器和一个 tableview 控制器。 mapviewcontroller 很好。我希望按距离对表格视图单元格进行排序。目前,单元格由 self.stores 数组中的 Store 对象填充。存储对象显然没有距离(到 userLocation)属性。因此,在 mapviewcontroller 中,我使用了该 plotStores 方法,在该方法中,我将 db fetch 放入 self.stores 数组中,并通过它循环使用具有距离属性的 MyLocation NSObject 来创建 MKannotations。有没有办法只访问那些 MyLocation 对象来对单元格进行排序?
-
什么类有
self.stores? -
都是mapviewcontroller,这是上面的plot方法的来源。我在 tableviewcontroller 中再次这样做。我实际上在两个视图控制器的 viewDidLoad 方法中重复了一个 db fetch 并将其结果放入 NSArray self.stores。
标签: ios custom-object