提示,这是一个按距离对字典进行排序的代码,我需要将一个变量coord传递给detailview,将其分成位置并将标记放在karte上。否则为什么coord给0以及如何将它传递给坐标?
- (void)viewDidLoad
{[self setupArray];
[super viewDidLoad];}
-(void)setupArray{ self.myLocationManager = [[CLLocationManager alloc] init];
[states setObject: @12 forKey:@"60.050043,30.345783"];
[states setObject: @11 forKey:@"60.037389,30.322094"];
[states setObject: @32 forKey:@"60.037329,30.322014"];
[states setObject: @1 forKey:@"59.957387,30.324681"];
NSLog(@"%f",betweenDistance);
NSArray* sortedStates = [states keysSortedByValueUsingComparator: ^(id obj1, id obj2)
{
if ([obj1 floatValue] > [obj2 floatValue])
{
return (NSComparisonResult)NSOrderedDescending;
}
if ([obj1 floatValue] < [obj2 floatValue])
{
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSLog(@"%@", sortedStates);
NSMutableArray* rows = [[NSMutableArray alloc] init];
for (NSString* key in sortedStates)
{
CGFloat distance = [[states objectForKey:key] floatValue];
[rows addObject:[NSString stringWithFormat:@"Distance is %f km", distance]];
}
datasource=rows;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Detail2ViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
[self.navigationController pushViewController:detail animated:YES];
NSString* coord = sortedStates[indexPath.row];
NSLog(@"virable coord %@",coord);
}
更新
2015-04-23 13:25:07.361 gfhfgh[503:7361] 8845784.000000
2015-04-23 13:25:07.363 gfhfgh[503:7361] (
"59.957387,30.324681",
"60.037389,30.322094",
"60.050043,30.345783",
"60.037329,30.322014"
)
2015-04-23 13:25:07.620 gfhfgh[503:7361] Google Maps SDK for iOS version: 1.9.14591.0
2015-04-23 13:25:10.294 gfhfgh[503:7361] as (null) it is virable coord
2015-04-23 13:25:10.296 gfhfgh[503:7361] my viraible (null)
2015-04-23 13:25:10.296 gfhfgh[503:7361] ((null)) was false: provideAPIKey: should be called at most once
UPDATE2 我使用这个功能,可能是因为它不起作用?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
//---------- CELL BACKGROUND IMAGE -----------------------------
UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.frame];
UIImage *image = [UIImage imageNamed:@"LightGrey.png"];
imageView.image = image;
cell.backgroundView = imageView;
[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
[[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];
cell.textLabel.text = [datasource objectAtIndex:indexPath.row];
//Arrow
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
答案 0 :(得分:0)
在[self setupArray]
之前致电[super viewDidLoad]
并不是一个好习惯。先叫它。
在- (void)reloadData
上加载数据后,尝试调用UITableView的方法- (void)setupArray
,告诉tableView的委托您已加载或更新了数据,并希望在tableView上显示它。它应该可以解决你的问题。
仅供参考:在您的情况下,numberOfRowsInSection:
应该返回[sortedStates count]
以确保点击单元格时,您的应用不会崩溃。
<强>更新强>
- (void)viewDidLoad
{
[super viewDidLoad];
//Call setup array after [super viewDidLoad]
[self setupArray];
}
-(void)setupArray {
//Do all your setupArray stuff
//Now update the table contents
[yourTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//Use [sortedStates count] here to be sure your app will not crash later
return [sortedStates count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Detail2ViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
[self.navigationController pushViewController:detail animated:YES];
NSString* coord = sortedStates[indexPath.row];
NSLog(@"virable coord %@",coord);
}
更新2
我想我找到了原因!你有一个名为sortedStates的实例变量,但在- (void)setupArray
内你声明一个同名的局部变量,这就是你没有在其他方法中获取值的原因。
尝试更改
NSArray* sortedStates = [states keysSortedByValueUsingComparator: ^(id obj1, id obj2) {
//stuff
}];
与
//without "NSArray *"
sortedStates = [states keysSortedByValueUsingComparator: ^(id obj1, id obj2) {
//stuff
}];
答案 1 :(得分:-1)
因为sortedStates [indexPath.row];将导致NSDictionary。 所以代码应该是:
NSDictionary *coord = sortedStates[indexPath.row];
将此值传递给detailViewController并在那里提取坐标。