didSelectRowAtIndexPath not working with NSNotificationCenter

一世执手 提交于 2019-12-11 16:53:25

问题


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:

In the 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];

    }];
}

and I collect that info in my DetailViewController.m like so:

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

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

}

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

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.

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.


回答1:


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

And in your DetailViewController :

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

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

}



来源:https://stackoverflow.com/questions/17479737/didselectrowatindexpath-not-working-with-nsnotificationcenter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!