2

Dear StackOverflow users,

First Post so will try my best!

I have a simple JSON file which looks like this(I won't include all of it because it's too long):

{
"guidelines": [
{
  "title": "Editorial - Doporučené postupy",
  "guidelinepath": "1 - Editorial"
},
{
  "title": "Preambule",
  "guidelinepath": "1 - Preambule"
},
{
  "title": "Zásady dispenzární péče ve fyziologickém těhotenství",
  "guidelinepath": "1- Z"
},
{
  "title": "Provádění screeningu poruch glukózové tolerance v graviditě",
  "guidelinepath": "2"
}]
}

With this data, I managed to populate a tableView(that is, the JSON parsed correctly in the command line output), hurray for me. Now what I would like to do is to detect the tableView cell which has been tapped and direct to the guidelinepath JSON object which is related to that title(this will lead to a text file which will populate a text view).I've tried a number of different solutions but they have resulted in (null).

The following is the incomplete code which I have managed to complete and which is error free.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showGuideline"]) {
    NSLog(@"seguehasbeenselected");
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    NSLog(@"%@ is the indexpath", indexPath);
}
}

I have tried to research the question thoroughly and tried to help myself with the following answers: Using indexPath.row to get an object from an array

getting json object and then assign to uitable view

But they couldn't really answer my question somehow.Any help would be really appreciated!

For those who would like to get more information about how the JSON was parsed, the following is the code:

{
[super viewDidLoad];
NSString *jsonFilePath = [[NSBundle mainBundle] pathForResource:@"postupy" ofType:@"json"];
NSData *jsonData = [NSData dataWithContentsOfFile:jsonFilePath];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(@"%@",dataDictionary);
self.guidelines = [dataDictionary objectForKey:@"guidelines"];
self.guidelineFiles = [dataDictionary objectForKey:@"guidelinepath"];
}

Declaration of guidelineFiles and guidelines:

#import <UIKit/UIKit.h>


@interface PostupyTableViewController : UITableViewController

@property (nonatomic, strong) NSArray *guidelines;
@property (nonatomic, strong) NSArray *guidelineFiles;


@end

-- FINAL SOLUTION --

I edited the prepareForSegue method as follows and it now works perfectly:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{
    if ([segue.identifier isEqualToString:@"showGuideline"]) {
        NSLog(@"seguehasbeenselected");
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSLog(@"%@ is the indexpath", indexPath);
        NSDictionary *item = [self.guidelines objectAtIndex:indexPath.row];
        NSString * path = [item objectForKey :@"guidelinepath"];
        NSLog(@"%@ is the path",path);
        PostupyDetailViewController *pdwc = (PostupyDetailViewController *)segue.destinationViewController;
        pdwc.guidelineChosen = path;
    }
}
4
  • +1 for nice formatting. Welcome to SO Commented Dec 4, 2013 at 10:21
  • Thank you much appreciated :)! Looking forward to solving this challenge :) Commented Dec 4, 2013 at 10:24
  • Show your declaration of guidelineFiles. Commented Dec 4, 2013 at 10:28
  • @stefbmt 'didSelectRowAtIndexPath' will get called after 'prepareForSegue'. That is ther reason there index path is null. Commented Dec 4, 2013 at 10:28

1 Answer 1

1

From your json it seems that [dataDictionary objectForKey:@"guidelines"]; returns an NSArray. So access to the right item just use:

NSDictionary *item = [self.guidelines objectAtIndex:indexPath.row];
NSString     *path = [item objectForKey:@"guidelinepath"];
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Mat, Thanks so much for your help.I tried to NSLog "path" , but it didn't seem to output anything. i.e the last log that was outputted was "..is the index path"
You're welcome, keep in mind that in the Json arrays are included between "[ ]" and dictionaries (key-value elements) between "{ }".
Added your two lines and transported the path instance to the DetailViewController but didn't manage to get it to work: NSDictionary *item = [self.guidelineFiles objectAtIndex:indexPath.row]; NSString * path = [item objectForKey :@"guidelinepath"]; NSLog(@"%@ is the path",path); PostupyDetailViewController *pdwc = (PostupyDetailViewController *)segue.destinationViewController; pdwc.guidelineChosen = path;
Hi @Mat ! Just realized that wrote guidelineFiles instead of guidelines and now it works like a charm! Thank you thank you thank you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.