didSelectRowAtIndexPath无法与NSNotificationCenter一起使用 [英] didSelectRowAtIndexPath not working with NSNotificationCenter

查看:138
本文介绍了didSelectRowAtIndexPath无法与NSNotificationCenter一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a MasterDetail app that I am working on that will be used to show players (as in sports) and detail stats. The list of players is called from a Postgres Database and parsed from JSON using the JSONModel. So far, I am able to get all of the data that I need from the Postgres DB, and display it perfectly in the MasterView. I am using the NSNotificationCenter to pass the data from the Master to the Detail view (I fetch the data using a function in the MasterView). I am able to pass the data accurately to the Detail view, but for some reason, my didSelectRowAtIndexPath is not working right. I obviously did something wrong, but I have no idea what it is. Here are the important parts of the code:

在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

and then my didSelectRowAtIndexPath in my 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的代码,请告诉我。

There you have it. If you want me to post more or if you want code from the DetailViewController.m, MasterViewController.h, or PlayerSelectionDelegate.h, just let me know.

作为备注,我最初是根据Ray Wenderlich iPad SplitView应用程序教程创建的。是的,我是新手。

As a note, I originally created this based off of the Ray Wenderlich iPad SplitView app tutorial a while back. And yes, I am new to this all.

推荐答案

您需要将NSNotification放入

You need to place the NSNotification in

 - (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中:

And in your DetailViewController :

- (void)handleNotification:(NSNotification *) notification
{
NSLog(@"%@", notification.userInfo);

 nameLabel.text = [notification.userInfo objectForKey:@"name"];

}

这篇关于didSelectRowAtIndexPath无法与NSNotificationCenter一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆