从PLIST获取数据到表3

 手机用户2702938421 发布于 2023-02-08 15:13

我一直在做一个用plist填充表格的导航堆栈.但是,当我单击"城市"时,我似乎可以将"名称"信息传递给我的第三个表,因为没有显示任何内容.我确实得到了一个新的弹出表,但它应该填充"大峡谷",但事实并非如此.我已成功从"目的地"传递到"城市"但不是"名称".我认为问题在于开始循环不能识别写入"名称"的键.

plist中:





    
        Destination
        Arizona
        Tours
        
            
                City
                Phoenix
                Options
                
                    
                        Name
                        Grand Canyon
                    
                
            
        
    
    
        Destination
        California
        Tours
        
            
                City
                San Diego
            
            
                City
                Calexico
            
        
    


码:

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

Janak Nirmal.. 6

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


FluffulousCh.. 5

这段代码很可疑:

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的代码播种你的应用程序; 但是,如果您想通过Web API下载游览,或允许用户对游览或其他任何内容发表评论,会发生什么.plist-as-model-layer方法使这很困难.其他人也难以获取代码并阅读它.通过显式对象类比使用包含字典的数组的字典数组更容易处理对象关系等.

2 个回答
  • 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.
            }
        }
    }
    

    2023-02-08 15:15 回答
  • 这段代码很可疑:

    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的代码播种你的应用程序; 但是,如果您想通过Web API下载游览,或允许用户对游览或其他任何内容发表评论,会发生什么.plist-as-model-layer方法使这很困难.其他人也难以获取代码并阅读它.通过显式对象类比使用包含字典的数组的字典数组更容易处理对象关系等.

    2023-02-08 15:15 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有