使用 UIPickerView 从 P 列表中获取 NSDictionary

发布于 2024-10-12 03:42:20 字数 2072 浏览 9 评论 0原文

我的一个应用程序中的选择器出现问题。我有一个从属性列表中获取的 NSDictionary,其中包含一堆键,而这些键又包含一堆字符串。我有两个组件,每个组件都应具有相同的字符串列表。我还有一个滑块,我想用它来允许用户更改键。因此,当滑块的值从 0 变为 1 时,字典中索引 1 处的键应将其内容加载到 pickerview 的组件中。

它的工作原理是根据滑块将新内容加载到选择器中。我一直使用滑块的标签作为变量来指示加载哪些内容。问题是,在加载新的项目列表后,程序崩溃了,我认为所需的行数没有得到更新或其他什么,但我对 UIPickerView 的经验不够,无法自己隔离问题,而无需花费更多比我自己尝试解决这个问题花费的时间还要多。

以下是我的选择器的委托/数据方法:

#pragma mark -
#pragma mark Picker Delegate/Data Methods

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

        //aryNames is an NSArray instance variable that contains the values for the components of the picker

    if (component == 0)
            return [self.aryNames count];
    return [self.aryNames count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row 
forComponent:(NSInteger)component
{
        //I think this is where my problem is
        //I'm using a string to select the object
        // at the index of the slider's location to 
        // fill up the instance variable with new data.

        //Anyway, it works fine if I have two different arrays hardcoded
        //but I'd really like to have this load dynamically because
        //there are a lot of keys and this way I could add and remove keys without
        //worrying about changing code

    NSString *selectedType = [self.aryKeys objectAtIndex:slideUnitTypes.tag];
    NSArray *newNames = [dictAllNames objectForKey:selectedType];
    self.aryNames = newNames;

    return [aryNames objectAtIndex:row];
}


//I'm pretty sure that the method below is not the problem

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:
(NSInteger)component
{
    if (component == 0) 
    { 
        [firstValueHeading setText:[aryNames objectAtIndex:row]]; 
    }
    else 
    { 
        [secondValueHeading setText:[aryNames objectAtIndex:row]]; 
    }
}

如果描述性不够或者您需要查看更多代码,请告诉我。这个问题对于原本顺利的项目来说是一个真正的麻烦。谢谢。

I'm having a problem with my picker in one of my apps. I have an NSDictionary obtained from a property list that contains a bunch of keys, which in turn contain a bunch of strings. I have two components, each one should have the same list of strings within. I also have a slider that I want to use to allow the user to change keys. So when the slider's value goes from 0 to 1 the key at index 1 in the dictionary should load its contents into the pickerview's components.

It's working as far as loading the new contents into the picker based on the slider. I've been using the slider's tag as the variable to dictate which contents get loaded. The problem is that after loading a new list of items the program crashes, I'm thinking that the number of rows needed isn't getting update or something but I'm just not experienced enough with UIPickerView to isolate the problem myself without spending more hours than I've already used trying to figure this out myself.

Here are my delegate/data methods for the picker:

#pragma mark -
#pragma mark Picker Delegate/Data Methods

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

        //aryNames is an NSArray instance variable that contains the values for the components of the picker

    if (component == 0)
            return [self.aryNames count];
    return [self.aryNames count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row 
forComponent:(NSInteger)component
{
        //I think this is where my problem is
        //I'm using a string to select the object
        // at the index of the slider's location to 
        // fill up the instance variable with new data.

        //Anyway, it works fine if I have two different arrays hardcoded
        //but I'd really like to have this load dynamically because
        //there are a lot of keys and this way I could add and remove keys without
        //worrying about changing code

    NSString *selectedType = [self.aryKeys objectAtIndex:slideUnitTypes.tag];
    NSArray *newNames = [dictAllNames objectForKey:selectedType];
    self.aryNames = newNames;

    return [aryNames objectAtIndex:row];
}


//I'm pretty sure that the method below is not the problem

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:
(NSInteger)component
{
    if (component == 0) 
    { 
        [firstValueHeading setText:[aryNames objectAtIndex:row]]; 
    }
    else 
    { 
        [secondValueHeading setText:[aryNames objectAtIndex:row]]; 
    }
}

If it wasn't descriptive enough or you need to see more of my code please tell me. This problem has been a real bugger in an otherwise smooth project. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

面如桃花 2024-10-19 03:42:20

我自己对此还很陌生,但在 Troy Brant 的书中(第 9 章)他这样做了。您应该从图书馆/书店获取这本书并在 http://troybrant 处查看源代码.net/iphonebook/chapter9/Ruralfork-done.zip

它应该有帮助。

I am still fairly new to this myself, but in Troy Brant's book (chapter 9) he does this. You should grab the book from the library/bookstore and review the source code at http://troybrant.net/iphonebook/chapter9/Ruralfork-done.zip

It should help.

初见终念 2024-10-19 03:42:20

我实际上很久以前就解决了这个问题。这是该代表的代码(如果有帮助的话):

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{ 
    NSUInteger index = slideUnitTypes.value;
    NSString *placeString = [self.aryKeys objectAtIndex:index];
    NSArray *returnThisArray = [dictAllNames objectForKey:placeString];

    return [returnThisArray objectAtIndex:row];
}

如果有人需要查看我的其他代表,只需对此答案发表评论,希望应该给我发送电子邮件。

i've actually solved this long since. here is the code for that delegate if it helps:

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{ 
    NSUInteger index = slideUnitTypes.value;
    NSString *placeString = [self.aryKeys objectAtIndex:index];
    NSArray *returnThisArray = [dictAllNames objectForKey:placeString];

    return [returnThisArray objectAtIndex:row];
}

if anyone out there needs to see any of my other delegates just comment on this answer and hopefully SO should send me an email.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文