我有一个我正在制作的MasterDetail应用程序,用于显示玩家(如运动)和详细统计数据。从Postgres数据库调用播放器列表,并使用JSONModel从JSON解析。到目前为止,我能够从Postgres DB获取所需的所有数据,并在MasterView中完美显示。我正在使用NSNotificationCenter将数据从Master传递到Detail视图(我使用MasterView中的函数获取数据)。我能够准确地将数据传递到Detail视图,但由于某种原因,我的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应用程序教程创建的。是的,我是这一切的新手。
答案 0 :(得分:1)
您需要将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"];
}