Uitableview 如何将MapViewController设置为TableViewController的数据源,以便在单元格中显示距离
我的应用程序有一个带有UIViewController的tabbarcontroller(FirstViewController称之为mapVC)和一个带有mapview的UITableViewController(SecondViewController称之为tableVC)。该应用程序从web获取数据并将其放入CD db中,每个VC执行对db的获取。每个实体都被命名为Holiday(Don ask),并且它有一个lat和long属性 下面是UITabBarController子类,它试图将mapVC设置为tableVC的数据源:Uitableview 如何将MapViewController设置为TableViewController的数据源,以便在单元格中显示距离,uitableview,mkmapview,distance,Uitableview,Mkmapview,Distance,我的应用程序有一个带有UIViewController的tabbarcontroller(FirstViewController称之为mapVC)和一个带有mapview的UITableViewController(SecondViewController称之为tableVC)。该应用程序从web获取数据并将其放入CD db中,每个VC执行对db的获取。每个实体都被命名为Holiday(Don ask),并且它有一个lat和long属性 下面是UITabBarController子类,它试图将m
- (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 <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;
}
导入
#进口
@接口位置:NSObject{
NSString*\u名称;
NSString*\u地址;
CLLocationCoordinate2D_坐标;
}
@属性(副本)NSString*名称;
@属性(副本)NSString*地址;
@属性(非原子,只读)CLLocationCoordinate2D坐标;
@属性(分配)浮动距离;
-(id)initWithName:(NSString*)名称地址:(NSString*)地址坐标:(CLLocationCoordinate2D)坐标距离:(float)距离;
@结束
#导入“MyLocation.h”
@实施MyLocation
@合成名称=_名称;
@综合地址=_地址;
@综合坐标=_坐标;
@合成距离=_距离;
-(id)initWithName:(NSString*)名称地址:(NSString*)地址坐标:(CLLocationCoordinate2D)坐标距离:(float)距离{
if((self=[super init])){
_名称=[名称副本];
_地址=[地址副本];
_坐标=坐标;
_距离=距离;
}
回归自我;
}
-(NSString*)标题{
if([[u name iskindof类:[NSNull类]])
返回@“未知费用”;
其他的
返回_name;
}
-(NSString*)副标题{
返回[NSString stringWithFormat:@“A%0.2f Kms”,_距离];
//返回地址;
}
具体问题:
1) 如果我在mapVC(新数据源)中添加了cFRAIP(noris&nosit)方法,我需要从tableVC中删除它吗
2) 如果我从tableVC中删除cFRAIP和其他2个(noris和nosit)方法,它会崩溃,因为没有数据源。因此,数据源的tabbarcontroller分配似乎不起作用
3) 最后,如果我必须从tableVC中删除cFRAIP,我将失去在tableview中使用UISearchBar的能力。还是我错了
当我运行应用程序时,mapVC是所选的vc。在web获取完成之前,在mapVC中调用plotStoreLocations的工具栏中的UIButton将变灰。此时,控制台将记录self.farsiman位置,这些位置是假日实体。我可以看到所有实体都登录到控制台
单击“打印”按钮时,用户位置将按照plotStorePositions中的指定正确记录,并且每个注释的距离值都是正确的。因此,可以正确计算每个注释的距离
当我切换到tableVC选项卡时,控制台中会记录新的self.dates数组(因为我当前让tableVC执行另一个CD db提取。我会为每个pin位置获取:
已在CFRAIP中,位置为+/-
2013年1月24日下午8:20:39中央标准时间0.00米(速度-1.00 mps/航向-1.00),用户位置为(空)
这是由CFRAIP调用的CaleAdvestDistFoLAT调用的,并且每个单元格中的距离都是-0.001.
< P>你确实需要制作MMAVC的数据源和委托。如果UITableViewController不让你把它分配给其他自己的东西,你可能想考虑使用一个普通的UIViewController AN。d在其上放置UITableView。如果将其设置为名为tableview的属性,则tabbarcontroller中执行tableVC.tableview.dataSource=mapVC;
的代码仍然有效
是的,您可以并且应该从tableVC中删除所有tableview委托和数据源代码,如果它正在调用这些代码,那么它就不会调用您想要的mapVC中的代码
它可能给了您错误的距离,因为tableVC没有用户的测量位置。将table datasource附加到地图上的另一个很好的原因。我可能在开始一个新问题时误导了您。我认为您的代表和数据源正在工作,您所需要的只是帮助cFRAIP,以便每个单元格显示了右侧MyLocation的数据。好的,但是如果我从tableVC中删除cFRAIP,我的UISearchBar是否适用于tableVC?
另外,我是否应该能够从SecondViewController访问MyLocations对象?也许我可以用它们创建单元格?我搜索了导致我找到的“which take me to”(我找到了“”)其余的由您决定。另外,您的secondviewcontroller没有制作单元格,数据源(mapvc)isI已经在tableviewcontroller中实现了uISearchBar。您可以看看这个想法作为替代方案:
#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;
}
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;
}