0

私のアプリには、MAPViewとUITableViewController(SecondViewController-tableVCと呼びます)を備えたUIViewController(FirstViewController-mapVCと呼びます)を備えたタブバーコントローラーがあります。アプリはWebからデータをフェッチしてCD-dbに入れ、各VCはdbへのフェッチを実行します。各エンティティにはHoliday(質問しないでください)という名前が付けられ、latとlongのプロパティがあります。

次に、mapVCをデータソースとしてtableVCに設定しようとするUITabBarControllerサブクラスを示します。

- (void)viewDidLoad{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    FirstViewController *mapVC;
    SecondViewController *tableVC;

    for(UIViewController *anyVC in self.viewControllers)
    {
        if([anyVC.class isKindOfClass:[SecondViewController class]]){
            tableVC = (SecondViewController *)anyVC;
        } else if ([anyVC.class isKindOfClass:[FirstViewController class]]){
            mapVC = (FirstViewController *)anyVC;
        }
    }

    tableVC.tableView.dataSource = mapVC;
    tableVC.tableView.delegate = mapVC;
}

mapVCの関連部分は次のとおりです。

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "SecondViewController.h"

#define METERS_PER_MILE 2609.344

@interface FirstViewController : UIViewController <MKMapViewDelegate, UITableViewDataSource, UITableViewDelegate>{
    BOOL _doneInitialZoom;

}

@property (strong, nonatomic) IBOutlet MKMapView *_mapView;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *refreshButton;
@property (nonatomic, strong) NSString *entityName;
@property (strong, nonatomic) CLLocation *userLocation;

- (IBAction)refreshTapped:(id)sender;
-(void)showDetailView;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
@end

およびその実装:

#import "FirstViewController.h"
#import "Holiday.h"
#import "MyLocation.h"
#import "SDCoreDataController.h"
#import "MyTabBarController.h"
#import "TableViewCell.h"

- (void)loadRecordsFromCoreData {
    [self.managedObjectContext performBlockAndWait:^{
        [self.managedObjectContext reset];
        NSError *error = nil;
        NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:self.entityName];
        [request setSortDescriptors:[NSArray arrayWithObject:
                                     [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]]];
        self.farSiman = [self.managedObjectContext executeFetchRequest:request error:&error];

    }];
    NSLog(@"self.farSiman on launch = %@", self.farSiman);
}

- (void)plotStorePositions:(NSString *)responseString {

    for (id<MKAnnotation> annotation in _mapView.annotations) {
        [_mapView removeAnnotation:annotation];
    }

    NSLog(@"Dictionary is %@", self.farSiman);

    for (Holiday * holidayObject in self.farSiman) {

        NSString * latitude = holidayObject.latitude;
        NSString * longitude = holidayObject.longitude;
        NSString * storeDescription = holidayObject.name;
        NSString * address = holidayObject.address;


        CLLocationCoordinate2D coordinate;
        coordinate.latitude = latitude.doubleValue;
        coordinate.longitude = longitude.doubleValue;
        MyLocation *annotation = [[MyLocation alloc] initWithName:storeDescription address:address coordinate:coordinate distance:0];

        //
        CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
        //[(MyLocation*)[view annotation] coordinate].latitude longitude:[(MyLocation*)[view annotation] coordinate].longitude]];

        self.userLocation = [[CLLocation alloc] initWithLatitude:self._mapView.userLocation.coordinate.latitude longitude:self._mapView.userLocation.coordinate.longitude];
        NSLog(@"PLOT>>userLocation is %@", userLocation);

        CLLocationDistance calculatedDistance = [pinLocation distanceFromLocation:self.userLocation];
        annotation.distance = calculatedDistance/1000;
        NSLog(@"PLOT>>Distance to pin %4.0f", annotation.distance);
        //

        [_mapView addAnnotation:annotation];

    }

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.farSiman count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = nil;

    // Check to see whether the normal table or search results table is being displayed and set the Candy object from the appropriate array
    NSLog(@"Already in CFRAIP");

    static NSString *CellIdentifier = @"HolidayCell";
    if (cell == nil) {
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        //cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
    }
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    Holiday *holiday = [self.farSiman objectAtIndex:indexPath.row];
    cell.nameLabel.text = holiday.name;
    //cell.dateLabel.text = holiday.latitude;

    cell.dateLabel.text = [[self calculateDistanceForLat:[holiday.latitude doubleValue] Long:[holiday.longitude doubleValue]] stringValue];

    if (holiday.image != nil) {
        UIImage *image = [UIImage imageWithData:holiday.image];
        cell.imageView.image = image;
    } else {
        cell.imageView.image = nil;
    }
    return cell;
}

// Add new method above refreshTapped
- (NSNumber*)calculateDistanceForLat:(double)lati Long:(double)longi {
    double distancia;
    CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:lati longitude:longi];
    CLLocationDistance calculatedDistance = [pinLocation distanceFromLocation:self.userLocation];

    //test locations
    NSLog(@"pinLocations is %@, userLocation is %@", pinLocation, self.userLocation);

    distancia = calculatedDistance/1000;
    return [NSNumber numberWithDouble:distancia];
}

plotStoreLocationsメソッドは、mapVCツールバーのUIButtonからの唯一のメソッド呼び出しです。

tableVC(SecondViewController)について

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface SecondViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>

@property (nonatomic, strong) NSArray *dates;
@property (nonatomic, strong) NSString *entityName;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *refreshButton;

@property (strong,nonatomic) NSMutableArray *filteredResultsArray;
@property (strong,nonatomic) IBOutlet UISearchBar *resultsSearchBar;

@property (strong, nonatomic) CLLocation *userLocation;

- (IBAction)refreshButtonTouched:(id)sender;

そしてその実装:

- (void)loadRecordsFromCoreData {
    [self.managedObjectContext performBlockAndWait:^{
        [self.managedObjectContext reset];
        NSError *error = nil;
        NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:self.entityName];
        [request setSortDescriptors:[NSArray arrayWithObject:
                                     [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]]];
        self.dates = [self.managedObjectContext executeFetchRequest:request error:&error];
        NSLog(@"self.dates==%@",self.dates);
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [filteredResultsArray count];
    } else {
        return [self.dates count];
    }

    //return [self.dates count];
}

// Add new method above refreshTapped
- (NSNumber*)calculateDistanceForLat:(double)lati Long:(double)longi {
    double distancia;
    CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:lati longitude:longi];
    CLLocationDistance calculatedDistance = [pinLocation distanceFromLocation:self.userLocation];

    //test locations
    NSLog(@"pinLocations is %@, userLocation is %@", pinLocation, self.userLocation);

    distancia = calculatedDistance/1000;
    return [NSNumber numberWithDouble:distancia];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = nil;

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        static NSString *CellIdentifier = @"HolidayCell";
        if (cell == nil) {
            cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        Holiday *holiday = [filteredResultsArray objectAtIndex:indexPath.row];
        NSLog(@"the holiday is %@", holiday.name);
        cell.nameLabel.text = holiday.name;

    } else {

        static NSString *CellIdentifier = @"HolidayCell";
        if (cell == nil) {
            cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        Holiday *holiday = [self.dates objectAtIndex:indexPath.row];
        cell.nameLabel.text = holiday.name;

        cell.dateLabel.text = [[self calculateDistanceForLat:[holiday.latitude doubleValue] Long:[holiday.longitude doubleValue]] stringValue];

        if (holiday.image != nil) {
            UIImage *image = [UIImage imageWithData:holiday.image];
            cell.imageView.image = image;
        } else {
            cell.imageView.image = nil;
        }
    }
    return cell;
}

最後にMyLocation:

import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MyLocation : NSObject <MKAnnotation> {
    NSString *_name;
    NSString *_address;
    CLLocationCoordinate2D _coordinate;
}

@property (copy) NSString *name;
@property (copy) NSString *address;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (assign) float distance;

- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate distance:(float)distance;

@end


#import "MyLocation.h"

@implementation MyLocation
@synthesize name = _name;
@synthesize address = _address;
@synthesize coordinate = _coordinate;
@synthesize distance = _distance;

- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate distance:(float)distance{
    if ((self = [super init])) {
        _name = [name copy];
        _address = [address copy];
        _coordinate = coordinate;
        _distance = distance;
    }
    return self;
}

- (NSString *)title {
    if ([_name isKindOfClass:[NSNull class]]) 
        return @"Unknown charge";
    else
        return _name;
}

- (NSString *)subtitle {
    return [NSString stringWithFormat:@"A %0.2f Kms", _distance];
    //return _address;
}

特定の質問:

1)mapVC(新しいデータソース)にcFRAIP(noris&nosit)メソッドを追加した場合、tableVCから削除する必要がありますか?

2)cFRAIPと他の2つの(norisとnosit)メソッドをtableVCから削除すると、データソースがないためにクラッシュします。したがって、データソースのタブバーコントローラーの割り当ては機能していないようです。

3)最後に、tableVCからcFRAIPを削除する必要がある場合、テーブルビューをUISearchBarする機能が失われます。それとも私は間違っていますか?

アプリを実行すると、mapVCが選択されたVCになります。mapVCのplotStoreLocationsを呼び出すツールバーのUIButtonは、Webフェッチが終了するまでグレー表示されます。この時点で、コンソールは休日エンティティであるself.farsimanの場所をログに記録します。そして、すべてのエンティティがコンソールにログインしているのを見ることができます。

プロットボタンをクリックすると、userLocationはplotStorePositionsで指定されているように正しくログに記録され、各注釈の距離値は正しいです。したがって、各MKAnnotationの距離は正しく計算されます。

tableVCタブに切り替えると、新しいself.dates配列がコンソールに記録されます(現在、tableVCに別のCD-dbフェッチを実行させているためです。これは、ピンの場所ごとに取得されます。

すでにCFRAIPにありますpinLocationsは<+15.50288611、-88.02716389> +/- 0.00m(速度-1.00 mps /コース-1.00)@ 1/24 / 13、8:20:39 PM中部標準時、userLocationは(null)

これは、CFRAIPによって呼び出されるcalculateDistanceForLatから呼び出されます。また、セルの詳細のすべての距離は-0.001です。

4

1 に答える 1

0

mapVC をデータソースとデリゲートにする必要があります。UITableViewController で他のものに割り当てることができない場合は、通常の UIViewController の使用を検討し、UITableView をそれにドロップすることを検討してください。tableview と呼ばれるプロパティとして設定すると、tabbarcontroller のコードはtableVC.tableView.dataSource = mapVC;引き続き機能します。

はい、tableVC からすべての tableview デリゲートとデータソース コードを削除できますし、削除する必要があります。

tableVC には測定するユーザーの位置がないため、間違った距離が表示されている可能性があります。テーブル データソースをマップにアタッチするもう 1 つの正当な理由です。

于 2013-01-25T02:54:01.467 に答える