objective-c - 从 PLIST 中获取数据到表 3

标签 objective-c ios7 plist

我一直在做一个用 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>

代码:

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

最佳答案

Plist

上面是你在 Xcode 中显示的 plist 结构,让我帮你调试你的代码。

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

上面的代码将以数组的形式加载游览中的所有数据,即它将包含项目 0、项目 1 等等,让我们看看你的循环,

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

现在 tourDictionary 是字典,它包含 2 个对象 Destination(NSString)和 Tours(NSArray),所以现在要从 Tours 中获取选项,您必须这样做

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

关于objective-c - 从 PLIST 中获取数据到表 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20740180/

相关文章:

iphone - 如何在我的 iPhone 应用程序中启动 YouTube 视频?

ios - 架构 armv7 的 undefined symbol : "_OBJCCLASS$_ALAssetsLibrary"

iOS 7/8 系统键盘高度

objective-c - 从 plist 读取字典后无法对其进行编辑

iphone - 将 NSMutableDictionary 保存到 plist 问题

ios - 在相机 View 中显示 UIView

ios - 识别点击 iOS 网页内的链接

iphone - 将 UILabel 添加到 UIImageView

ios - 如何在 UITableView 中插入和删除行时模拟折纸

iphone - XML 到 NSdictionary 到 plist