简体   繁体   English

如何在Objective-c的.plist文件中显示同一键的多个参数?

[英]How can I display multiple parameters of the same key from a .plist File on Objective-c?

I have this arquivo.plist below. 我在下面有这个arquivo.plist
I looked at Apple's documentation, but I could not find how to make the listing look and I hope someone can help me. 我查看了Apple的文档,但是找不到如何使列表看起来更漂亮,希望有人可以帮助我。


In the first UITableView , I make a list of names present in arquivo.plist using the following code and loading the following method below. 在第一个UITableView ,我使用以下代码并在下面加载以下方法,列出了arquivo.plist存在的名称。

What I'm trying to do is: When the user clicks on a tableView name, he is taken to a second screen with a new tableView that will display the "frams" of that user. 我想做的是:当用户单击tableView名称时,他进入带有新tableView的第二个屏幕,该屏幕将显示该用户的“框架”。

The problem is that the code I'm using to select the "frams" is the same code that I am using for listing the names on the first screen, but it is not working, because each user has only one name, but it has several "frams". 问题是我用来选择“ frams”的代码与我用来在第一个屏幕上列出名称的代码相同,但是它不起作用,因为每个用户只有一个名字,但是几个“框架”。
When I try to implement this, I can even assemble the array with the "frams" and pass using segue (i'll show the array with an NSLog ), but this array can not be displayed as a tableView for an error that Xcode is giving me: 当我尝试实现此功能时,我什至可以将数组与“ frams”组合起来并使用segue传递(我将使用NSLog显示该数组),但是由于Xcode是错误,该数组不能显示为tableView 。给我:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary length]: unrecognized selector sent to instance 0x8b87a60' *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__ NSCFDictionary length]:无法识别的选择器已发送到实例0x8b87a60'

The code from the fist view (ViewController.m) is below the contatos.plist 拳头视图(ViewController.m)中的代码在contatos.plist下面

I hope you understand what I'm trying to do and bring me a light! 希望您了解我正在尝试做的事情,并给我带来启发! Thank you all! 谢谢你们!

a screenshot of the .plist file is bellow: .plist文件的屏幕截图如下:

http://img268.imageshack.us/img268/7356/jl26.png http://img268.imageshack.us/img268/7356/jl26.png

For reference and for you to better understand the issue, when the user clicks on a table name, an array is generated with "frams" this patient (patients have on average 3 frams each). 为了供您参考并更好地理解该问题,当用户单击表名称时,将生成一个包含该患者“ fram”的数组(每个患者平均有3个fram)。 The Array NSLog that it should be passed to the other page and comport a new table generates the following text: 应当将其传递到另一页并转换为新表的Array NSLog会生成以下文本:

 12/12/2013 16:10:13.513 rer [24251:70 b] (
{
    1 = 11;
    2 = 12;
    3 = 13;
}

I think that's where the problem lies, because this method of Array can not be implemented in a TableView, so I look a light of a better way to mount this array and select frams when I click a patient 我认为这就是问题所在,因为无法在TableView中实现此Array方法,因此我希望找到一种更好的方式来安装此数组并在单击患者时选择fram

So here is my ViewController.m: 所以这是我的ViewController.m:

#import "ViewController.h"
#import "Contato.h"
#import "perfilViewController.h"

@interface ViewController ()
-(void) loadContacts;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadContacts];
}

-(void) loadContacts
{
    NSString *plistCaminho = [[NSBundle mainBundle] pathForResource:@"contatos"
                                                             ofType:@"plist"];
    NSDictionary *pl = [NSDictionary dictionaryWithContentsOfFile:plistCaminho];
    NSArray *dados = [pl valueForKey:@"contatos"];

    contatos = [[NSMutableArray alloc] init];

    for (NSDictionary *item in dados) {
        NSString *nome = [item valueForKey:@"nome"];
        NSString *telefone = [item valueForKey:@"telefone"];
        NSString *fram = [item valueForKey:@"fram"];

        Contato *c = [[Contato alloc] initWithNome:nome andTelefone:telefone andFram:fram];
        [contatos addObject:c];
    }
}

#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return contatos.count;
}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CelulaContatoCacheID = @"CelulaContatoCacheID";
    UITableViewCell *cell = [self.tabelaContatos dequeueReusableCellWithIdentifier:CelulaContatoCacheID];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CelulaContatoCacheID];
    }
    Contato *contato = [contatos objectAtIndex:indexPath.row];
    cell.textLabel.text = contato.nome;
    return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Contato *contato = [contatos objectAtIndex:indexPath.row];
    NSString *msg = [NSString stringWithFormat:@"Fram:%@",contato.fram];

    fran = [[NSMutableArray alloc] init];
    fran = [NSMutableArray arrayWithObject:contato.telefone];

    NSLog(@"finalmente eu consegui essa porra!!!%@", fran);

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Contato"
                                                    message:msg
                                                   delegate:nil
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
    [alert show];
    [self.tabelaContatos deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"vai"]) {
        NSIndexPath *indexPath = [self.tabelaContatos indexPathForSelectedRow];
        perfilViewController *destViewController = segue.destinationViewController;
        Contato *contato = [contatos objectAtIndex:indexPath.row];

        fran = [[NSMutableArray alloc] init];
        fran = [NSMutableArray arrayWithObject:contato.fram];
        destViewController.frans = fran;
        NSLog(@"%@%@", fran, destViewController.frans);
    }
}
@end

I'd firstly do: 我首先要做的是:

//declare NSDictionary *pl; in the ViewController.h
pl = [NSDictionary dictionaryWithContentsOfFile:plistCaminho];

Then to display Names, i'd simply use the dictionary as such: 然后要显示名称,我将仅使用字典:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[pl objectForKey:@"contatos"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //...

    //following commented line not needed anymore
    //Contato *contato = [contatos objectAtIndex:indexPath.row];

    cell.textLabel.text = [[[pl objectForKey:@"contatos"]
                               objectAtIndex:indexPath.row]
                                objectForKey:@"nome"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //following commented line not needed anymore
    //Contato *contato = [contatos objectAtIndex:indexPath.row];

    //NOTE: fran is a dictionary now
    //change "fran" declaration in the ViewController.h to
    //NSDictionary *fran;
    fran = [[NSDictionary alloc] init];
    fran = [[pl objectForKey:@"contatos"]
               objectAtIndex:indexPath.row];

    NSLog(@"finalmente eu consegui essa porra!!!%@", fran);

    //...

    //don't deselectRow (the indexPathForSelectedRow will not work correctly later)
    //[self.tabelaContatos deselectRowAtIndexPath:indexPath animated:YES];
}

then on -prepareForSegue i'd pass the dictionary associated with the selected row: 然后在-prepareForSegue传递与所选行关联的字典:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"vai"]) {
        NSIndexPath *indexPath = [self.tabelaContatos indexPathForSelectedRow];
        perfilViewController *destViewController = segue.destinationViewController;

        //following commented line not needed anymore
        //Contato *contato = [contatos objectAtIndex:indexPath.row];

        //NOTE: fran is a dictionary now
        //change "fran" declaration in the ViewController.h to
        //NSDictionary *fran;  
        fran = [[NSDictionary alloc] init];
        fran = [[[pl objectForKey:@"contatos"]
                    objectAtIndex:indexPath.row]
                     objectForKey:@"fram"];

        //NOTE: destViewController.frans should be a dictionary
        //handle it appropriately
        destViewController.frans = fran;
    }
}

so basically, all dictionary use, no need for Contato *contato at all. 因此,基本上所有字典都使用,根本不需要Contato *contato

EDIT: As for your current method, you would be better off replicating the same structure. 编辑:至于您当前的方法,您最好复制相同的结构。
As in: 如:

[array of names] has {dictionary with user details} having a key that has {dictionary of frams} [名称数组] 具有 {具有用户详细信息的字典} ,其密钥具有 {framdictionary offrams}

I'd recommend you not using a dictionary to store your Fram values, use an array instead. 我建议您不要使用字典来存储Fram值,而应使用数组。 This way, you can get the Fram values like this: 这样,您可以像以下方式获取Fram值:

for (NSDictionary *item in dados) {
    NSString *nome = [item valueForKey:@"nome"];
    NSString *telefone = [item valueForKey:@"telefone"];
    NSArray *fram = [item valueForKey:@"fram"];

    Contato *c = [[Contato alloc] initWithNome:nome andTelefone:telefone andFram:[fram objectAtIndex:index]];
    [contatos addObject:c];    
}

Where index is an integer variable you'll have to work out how to handle. 如果index是整数变量,则必须弄清楚如何处理。

PS: be a little more polite when logging you app ;) PS:登录您的应用时要礼貌一些;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM