0

我有一个我正在开发的 MasterDetail 应用程序,它将用于显示球员(如运动)和详细统计数据。玩家列表从 Postgres 数据库调用,并使用 JSONModel 从 JSON 解析。到目前为止,我能够从 Postgres DB 中获取我需要的所有数据,并将其完美地显示在 MasterView 中。我正在使用 NSNotificationCenter 将数据从 Master 传递到 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"];

}

然后我didSelectRowAtIndexPath在我的 MasterViewController

#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 应用教程创建的。是的,我对这一切都很陌生。

4

1 回答 1

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"];

}

于 2013-07-05T07:47:39.530 回答