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