我的一个应用程序中的选择器出现问题。我有一个从包含一堆键的属性列表中获得的 NSDictionary,而这些键又包含一堆字符串。我有两个组件,每个组件都应该有相同的字符串列表。我还有一个滑块,我想用它来允许用户更改键。因此,当滑块的值从 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]];
}
}
如果描述性不够或者您需要查看我的更多代码,请告诉我。这个问题在原本顺利的项目中是一个真正的问题。谢谢。