1

在我的应用程序中,我获取一个核心数据库并将结果放入一个名为 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 控制器?

4

1 回答 1

0

当您使用 Core Data 时,您最好的选择是使用 aNSFetchedResultsController来填充您的表格视图。

然后,您需要的只是托管对象上下文,来自另一个控制器或应用程序委托。

于 2013-02-14T17:06:48.550 回答