1

アプリの 1 つでピッカーに問題があります。私は一連のキーを含むプロパティ リストから取得した NSDictionary を持っています。これには一連の文字列が含まれています。2 つのコンポーネントがあり、それぞれに同じ文字列のリストが含まれている必要があります。ユーザーがキーを変更できるようにするために使用したいスライダーもあります。したがって、スライダーの値が 0 から 1 になると、ディクショナリのインデックス 1 のキーがその内容をピッカービューのコンポーネントにロードする必要があります。

スライダーに基づいて新しいコンテンツをピッカーにロードする限り、機能しています。スライダーのタグを変数として使用して、どのコンテンツをロードするかを指示しています。問題は、プログラムがクラッシュするアイテムの新しいリストをロードした後、必要な行数が更新されていないか何かだと思っていますが、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]]; 
    }
}

説明が不十分だった場合、または私のコードをもっと見る必要がある場合は、教えてください。この問題は、それ以外の点ではスムーズなプロジェクトの真の厄介者でした。ありがとう。

4

2 に答える 2

1

私はまだこれにかなり慣れていませんが、Troy Brant の本 (第 9 章) で彼はこれを行っています。図書館/書店から本を入手し、http://troybrant.net/iphonebook/chapter9/Ruralfork-done.zipでソース コードを確認する必要があります。

それは役立つはずです。

于 2011-03-04T22:27:11.600 に答える
0

私は実際にこれを長い間解決してきました。それが役立つ場合、そのデリゲートのコードは次のとおりです。

-(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];
}

誰かが私の他の代理人に会う必要がある場合は、この回答にコメントしてください。うまくいけば、SOが私にメールを送ってください。

于 2011-03-05T16:59:36.680 に答える