0

我使用 prepareForSegue 方法将 location_id 从 LocationTableViewController 传递到 BeerTableViewController 以在点击位置单元格时获取位置啤酒结果。我的问题是 location_id 每隔一段时间才更新一次。

示例:当点击位置 1 时,它会加载该位置的啤酒。然后我点击后退按钮并点击位置 2,但它仍然加载位置 1 啤酒。如果我再次回击,然后再次点击位置 2,它会显示正确的位置 2 啤酒。

为什么需要两次尝试才能加载正确的 location_id?

LocationTableViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{  
    self.selected_location = indexPath.row;
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([[segue identifier] isEqualToString:@"beer"]){
        BeerTableViewController *beer = (BeerTableViewController *)[segue destinationViewController];
        Location *location = [self.location_results objectAtIndex:self.selected_location];
        beer.location_id = location.location_id;
        }
}

然后在 BeerTableViewController 我使用 location_id 来获取结果

BeerTableViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *path = [NSString stringWithFormat:@"locations/%@/beers.json", self.location_id];

    [[LocationApiClient sharedInstance] getPath:path parameters:nil
                                        success:^(AFHTTPRequestOperation *operation, id response) {
                                            NSLog(@"Response: %@", response);
                                            NSMutableArray *beer_results = [NSMutableArray array];
                                            for (id beerDictionary in response) {
                                                Beer *beer = [[Beer alloc] initWithDictionary:beerDictionary];
                                                [beer_results addObject:beer];

                                            }
                                            self.beer_results = beer_results;
                                            [self.tableView reloadData];
                                        }
                                        failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                            NSLog(@"Error fetching beers!");
                                            NSLog(@"%@", error);

                                        }];
}
4

1 回答 1

2

tableView:didSelectRowAtIndexPath将在之后 prepareForSegue:sender:调用,因此您在啤酒视图控制器上设置后将self.selected_location其设置为正确的行。location_id

这就是为什么您下次会看到正确的行。你的啤酒视图控制器总是落后一排。

在您的prepareForSegue:sender:中,如果您有对 UITableView 的引用,则可以直接从表视图中获取当前正确选择的行。

假设您的 中有一个@property 'tableView' LocationTableViewController,您可以使用[self.tableView indexPathForSelectedRow]

编辑

如果您LocationTableViewController是 的子类UITableViewController,那么您已经拥有此 @property tableView。在这种情况下,你prepareForSegue:sender:会看起来像这样:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([[segue identifier] isEqualToString:@"beer"]){

      BeerTableViewController *beer = (BeerTableViewController *)[segue destinationViewController];
      NSInteger selectedRow = [[self.tableView indexPathForSelectedRow] row];
      Location *location = [self.location_results objectAtIndex:selectedRow];
      beer.location_id = location.location_id;
    }
}
于 2013-03-09T21:14:32.910 回答