0

私は、(スポーツのように) プレーヤーと詳細な統計を表示するために使用される、私が取り組んでいる MasterDetail アプリを持っています。プレーヤーのリストは Postgres データベースから呼び出され、JSONModel を使用して JSON から解析されます。これまでのところ、Postgres DB から必要なすべてのデータを取得し、MasterView に完全に表示することができました。NSNotificationCenter を使用して、マスターから詳細ビューにデータを渡しています (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 に答える