我在我的一個應用程序中遇到了我的選擇器問題。我有一個NSDictionary從屬性列表中獲得,其中包含一堆鍵,而這些鍵又包含一串字符串。我有兩個組件,每個組件都應該有相同的字符串列表。我也有一個我想用來允許用戶更改鍵的滑塊。所以當滑塊的值從0變爲1時,字典中位於索引1的鍵應該將其內容加載到pickerview的組件中。與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]];
}
}
如果這還不夠的描述,或者您需要看到更多的我的代碼,請告訴我。這個問題一直是一個非常順利的項目中的一個真正的問題。謝謝。