2011-01-14 2 views
1

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

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

を教えてください、それは十分に説明的ではなかったか、あなたは私のコードの詳細を参照する必要がある場合。この問題は、そうでなければスムーズなプロジェクトでは本当に厄介なものでした。ありがとう。

答えて

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私にメールを送信する必要があります必要がある場合:それは場合に役立ちますここでそのデリゲートのためのコードです。

関連する問題