Retrieving data from PLIST in table 3

I am making a navigation stack that populates a table with plist. However, I can pass the “Name” information to my third table when I click “City” because nothing is displayed. I get a new pop-up table, but it should populate the Grand Canyon, but it’s not. I successfully moved from "Destination" to "City", but not to "Name". I think the problem is where the loop starts that does not recognize the key for the "Name" record.

Plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>Destination</key>
        <string>Arizona</string>
        <key>Tours</key>
        <array>
            <dict>
                <key>City</key>
                <string>Phoenix</string>
                <key>Options</key>
                <array>
                    <dict>
                        <key>Name</key>
                        <string>Grand Canyon</string>
                    </dict>
                </array>
            </dict>
        </array>
    </dict>
    <dict>
        <key>Destination</key>
        <string>California</string>
        <key>Tours</key>
        <array>
            <dict>
                <key>City</key>
                <string>San Diego</string>
            </dict>
            <dict>
                <key>City</key>
                <string>Calexico</string>
            </dict>
        </array>
    </dict>
</array>
</plist>

code:

#import "OptionsViewController.h"


@interface OptionsViewController ()

@property NSArray *options;

@end

@implementation OptionsViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Available Tours";

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

-(void)setToursAvailable:(NSString *)toursAvailable{
    if (_toursAvailable != toursAvailable) {
        _toursAvailable = toursAvailable;

        NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Destination" ofType:@"plist"];
        NSArray *tours =[NSArray arrayWithContentsOfFile:filePath];




        for (int i = 0; i < [tours count]; i++) {


            NSDictionary *tourDictionary = [tours objectAtIndex:i];
            NSString *tempTour = [tourDictionary objectForKey:@"City"];


            if ([tempTour isEqualToString:_toursAvailable]) {
                self.options = [tourDictionary objectForKey:@"Options"];


            }
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [self.options count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    //Fetch Options
    NSDictionary *opt = [self.options objectAtIndex:[indexPath row]];
    // Configure the cell...
    [cell.textLabel setText:[opt objectForKey:@"Name"]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}
+4
source share
2 answers

Plist

plist Xcode, .

NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Destination" ofType:@"plist"];
NSArray *tours =[NSArray arrayWithContentsOfFile:filePath];

, Item 0, Item 1 .., ,

for (int i = 0; i < [tours count]; i++) {
    NSDictionary *tourDictionary = [tours objectAtIndex:i];
}

tourDictionary 2 Destination ( NSString) Tours ( NSArray), , , ,

NSArray *allTours = [tourDictionary objectForKey:@"Tours"];

, ,

.

for(NSDictionary *allCities in allTours){
    if([[allCities objectForKey:@"City"] isEqualToString:_toursAvailable]){
        self.options = [allCities objectForKey:@"Options"];
        break; //You got your options so just exit this loop.
    }
}

, :

for (int i = 0; i < [tours count]; i++) {
    NSDictionary *tourDictionary = [tours objectAtIndex:i];
    NSArray *allTours = [tourDictionary objectForKey:@"Tours"];
    for(NSDictionary *allCities in allTours){
        if([[allCities objectForKey:@"City"] isEqualToString:_toursAvailable]){ //Assuming _toursAvailable is City 
            self.options = [allCities objectForKey:@"Options"];
            break; //You got your options so just exit this loop.
        }
    }
}
+6

:

for (int i = 0; i < [tours count]; i++) {
    NSDictionary *tourDictionary = [tours objectAtIndex:i];
    NSString *tempTour = [tourDictionary objectForKey:@"City"];
    if ([tempTour isEqualToString:_toursAvailable]) {
        self.options = [tourDictionary objectForKey:@"Options"];
    }
}

, , @"City" .

:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Destination" ofType:@"plist"];
NSArray *destinations = [NSArray arrayWithContentsOfFile:path];

[destinations enumerateObjectsUsingBlock:^(id destObj, NSUInteger idx, BOOL *stop) {
    NSUInteger index = [destObj[@"Tours"] indexOfObjectPassingTest:^BOOL(id tourObj, NSUInteger idx, BOOL *stop) {
        return [tourObj[@"City"] isEqualToString:_toursAvailable];
    }];
    if( index != NSNotFound ) {
        self.options = destObj[@"Tours"][index][@"Options"];
        *stop = YES;
    }
}];

, UITableViewController toursAvailable, OptionsViewController. , .

, , :

. , , .. ? , plist; , -API - . . - . , , , ..

+5

All Articles