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