Objective-C有选择地从plist填充表;如果键等于 [英] Objective-C selectively populate table from plist; if key equals

查看:73
本文介绍了Objective-C有选择地从plist填充表;如果键等于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

plist1是40个字典的数组,每个字典有4个字符串. plist2也是一个数组,但是有120个字典,每个字典有2个字符串.

plist1 is an array of 40 dictionaries, with 4 strings each. plist2 is also an array, but of 120 dictionaries with 2 strings each.

如果我在一个表中列出了一个曲棍球联赛的所有球队(从plist1中的某个键填充),说我选择第五个.这将推一张新桌子. plist2中的120个词典有两个字符串-每个字符串顺序代表联盟历史上一年的时间,一个顺序则代表当年夺冠的人.我希望第二个表仅列出字典第二个键与在第一个表plist1中刚选择的团队名称相同的年份.因此,当我从第一张表中选择"5"队时,我希望只能看到第二张表中的"5"队获胜的年份.想法是所有年份都将出现在plist2中,但无关的年份将在移动中的实际表中被阻止.

If I list all the teams in a hockey league in a table (populated from a certain key in plist1), say I choose the fifth one down. This pushes a new table. The 120 dictionaries in plist2 have two strings - each has one for a year of the league's history in order, and one for who won the championship that year. I want this second table to list only the years for which the the dictionary's second key is the same team name that was just selected in the first table, plist1. So, when I select team "5" from the first table, I'd expect to see only the years that team "5" won in the second table. The idea is that all the years will be present in plist2, but the irrelevant ones will be blocked in the actual table on-the-go.

我怎么说:如果用户选择了X队,那么仅显示X队的年份,而不是所有年份."

How can I say "if the user selected team X, then show the years just for team X, not all the years."

谢谢!

推荐答案

实际上,您可以使用NSPredicate很好地进行这种过滤:

Actually you can do this kind of filtering quite nicely using NSPredicate:

NSDictionary *testA = [NSDictionary dictionaryWithObjectsAndKeys:@"valA", @"key1", @"1", @"key2", nil];
NSDictionary *testB = [NSDictionary dictionaryWithObjectsAndKeys:@"valB", @"key1", @"2", @"key2", nil];
NSDictionary *testC = [NSDictionary dictionaryWithObjectsAndKeys:@"valC", @"key1", @"1", @"key2", nil];
NSDictionary *testD = [NSDictionary dictionaryWithObjectsAndKeys:@"valD", @"key1", @"3", @"key2", nil];

NSArray *testArr = [NSArray arrayWithObjects:testA, testB, testC, testD, nil];

NSPredicate *pred = [NSPredicate predicateWithFormat:@"key2 == %@", @"1"];
NSArray *testFilter = [testArr filteredArrayUsingPredicate:pred];

NSLog(@"%@", testFilter);

此设置将创建4个字典及其数组.过滤器表达式现在搜索所有包含的对象,其中key2等于1,应为testAtestC.瞧,输出:

This setup creates 4 dictionaries and an array of them. The filter expression now searches for all contained objects where key2 is equal to 1, which should be testA and testC. And voila theres the output:

2011-11-24 18:57:07.822 testapp[9977:f803] (
        {
        key1 = valA;
        key2 = 1;
    },
        {
        key1 = valC;
        key2 = 1;
    }
)

这样,您可以创建谓词以按团队名称进行过滤,并创建数组的过滤版本.通常,这适用于所有支持键值编码的对象(NSDictionary可以这样做,有关更多信息,请参见:

This way you can create a predicate to filter by your team name and create a filtered version of your array. Generally this works on all kinds of object that support key-value coding (which NSDictionary does, further info on that: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/BasicPrinciples.html#//apple_ref/doc/uid/20002170)

这篇关于Objective-C有选择地从plist填充表;如果键等于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆