didSelectRowAtIndexPath不能与NSNotificationCenter一起使用

我有一个MasterDetail应用程序,我正在使用这将用于显示玩家(如体育)和详细统计。 玩家列表从PostgreSQL数据库调用,并使用JSONModel从JSONparsing。 到目前为止,我能够从Postgres DB获取所需的所有数据,并在MasterView中完美显示。 我正在使用NSNotificationCenter将数据从主数据传递到Detail视图(我使用MasterView中的函数获取数据)。 我能够将数据准确地传递给详细信息视图,但由于某种原因,我didSelectRowAtIndexPath不能正常工作。 我显然做错了,但我不知道它是什么。 这里是代码的重要部分:

在MasterViewController.m中的viewDidAppear:

-(void)viewDidAppear:(BOOL)animated { //fetch the feed from the Postgres Database [JSONHTTPClient getJSONFromURLWithString:@"http://myurl" completion:^(NSDictionary *json, JSONModelError *err) { NSError* error = nil; _feed = [[PostgresFeed alloc]initWithDictionary:json error:&error]; //Print the data fethced to NSLog in JSON format NSLog(@"Players: %@", _feed.players); [[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:nil userInfo:json]; //reload the table view after data collected [self.tableView reloadData]; }]; } 

我在我的DetailViewController.m中收集信息如下:

 - (void)handleNotification:(NSNotification *) notification { NSLog(@"%@", notification.userInfo); NSArray *playerData = [notification.userInfo objectForKey:@"player"]; NSDictionary *firstElement = [playerData objectAtIndex:0]; nameLabel.text = [firstElement objectForKey:@"name"]; } 

然后我在我的MasterViewController didSelectRowAtIndexPath

 #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; Player *selectedPlayer = [_players objectAtIndex:indexPath.row]; if (_delegate) { [_delegate selectedPlayer:slectedPlayer]; } } 

你有它。 如果您希望我发布更多内容,或者需要从DetailViewController.m,MasterViewController.h或PlayerSelectionDelegate.h中获取代码,请告诉我。

作为一个说明,我最初是基于Ray Wenderlich iPad SplitView应用程序教程创build的。 是的,我对这一切都是陌生的。

你需要放置NSNotification

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; //Here you have the NSDictionary from the json [JSONHTTPClient getJSONFromURLWithString:@"http://myurl" completion:^(NSDictionary *json, JSONModelError *err) { NSError* error = nil; _feed = [[PostgresFeed alloc]initWithDictionary:json error:&error]; //Print the data fethced to NSLog in JSON format NSLog(@"Players: %@", _feed.players); [JSONHTTPClient getJSONFromURLWithString:@"http://myurl" completion:^(NSDictionary *json, JSONModelError *err) { NSError* error = nil; _feed = [[PostgresFeed alloc]initWithDictionary:json error:&error]; //Print the data fethced to NSLog in JSON format NSLog(@"Players: %@", _feed.players); //Assuming that you have the players in the same order as your list [[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:nil userInfo:[[json objectForKey:@"players"]objectAtIndex:indexPath.row]]; }]; Player *selectedPlayer = [_players objectAtIndex:indexPath.row]; if (_delegate) { [_delegate selectedPlayer:slectedPlayer]; } } 

在你的DetailViewController中:

 - (void)handleNotification:(NSNotification *) notification { NSLog(@"%@", notification.userInfo); nameLabel.text = [notification.userInfo objectForKey:@"name"]; 

}