indexPath.row总是给出零(为什么不工作?)



提示符,这是一个按距离对字典进行排序的代码,我需要将变量坐标传递给详细视图,在那里它被分隔到位置并将标记放在卡丁车上。不,为什么坐标给出 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;
}

[super viewDidLoad]之前调用[self setupArray]不是一个好习惯。之前叫它。

尝试在- (void)setupArray上加载数据后调用 UITableView 的方法- (void)reloadData告诉 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
}];

这是因为sortedStates[indexPath.row];将导致NSDictionary。所以代码应该是:

NSDictionary *coord = sortedStates[indexPath.row];

将此值传递给您的详细视图控制器并提取其中的坐标。

最新更新