从NSUserDefault读取字典类型PSMultiValueSpecifier到UITableView

时间:2009-08-18 03:43:25

标签: iphone uitableview nsuserdefaults

有谁知道如何轻松地将NSUserDefault中的Dictionary类型PSMultiValueSpecifier读入UITableView控件并使用iPhone SDK 3.0将更改保存回NSUserDefaults?如果是这样,你可以发布一些特定的代码来有效地做到这一点吗?

1 个答案:

答案 0 :(得分:0)

首先要更好地访问bundle中的说明符值,我使用下面的代码将它们变成字典。

NSString* settingsBundle = [[[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"] stringByAppendingPathComponent:@"Root.plist"];

NSDictionary* rootPlist = [NSDictionary dictionaryWithContentsOfFile:settingsBundle];

if (rootPlist == nil)
    return nil;

NSArray* specifiers = [rootPlist objectForKey:@"PreferenceSpecifiers"];

NSDictionary *multiValueSpecifier = nil;

for (NSDictionary *specifier in specifiers)
{
    if ([[specifier objectForKey:@"Key"] isEqualToString:speficierKey] == YES &&
        [[specifier objectForKey:@"Type"] isEqualToString:@"PSMultiValueSpecifier"] == YES)
    {
        multiValueSpecifier = specifier;
        break;
    }
}

if (multiValueSpecifier == nil)
    return nil;

NSArray* titlesArray = [multiValueSpecifier objectForKey:@"Titles"];
NSArray* valuesArray = [multiValueSpecifier objectForKey:@"Values"];

NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:titlesArray 
                                                       forKeys:valuesArray];

return dictionary;

NSDictionary* rootPlist = [NSDictionary dictionaryWithContentsOfFile:settingsBundle]; if (rootPlist == nil) return nil; NSArray* specifiers = [rootPlist objectForKey:@"PreferenceSpecifiers"]; NSDictionary *multiValueSpecifier = nil; for (NSDictionary *specifier in specifiers) { if ([[specifier objectForKey:@"Key"] isEqualToString:speficierKey] == YES && [[specifier objectForKey:@"Type"] isEqualToString:@"PSMultiValueSpecifier"] == YES) { multiValueSpecifier = specifier; break; } } if (multiValueSpecifier == nil) return nil; NSArray* titlesArray = [multiValueSpecifier objectForKey:@"Titles"]; NSArray* valuesArray = [multiValueSpecifier objectForKey:@"Values"]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:titlesArray forKeys:valuesArray]; return dictionary;

现在好了,如果从您的UI中选择了存储KEY所需的内容而不是字典的值。