我在我的示例应用程序中使用了UIPickerView,我遇到了这个问题,除了第一行之外它没有重新加载数据。我实际编码分别独立分配选择器的第一行,但其他行加载数组。 UIPickerView的值应该在每次用户按下按钮时重新加载,但除了第一行之外没有任何值按预期改变,我无法弄清楚我出错的地方。以下是我的代码
//PickerView Code....
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
if (pickerView.tag == 1) {
return 1;
}
else if (pickerView.tag == 2)
{
return 1;
}
return 0;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView.tag == 1) {
return [self.CityList count];
}
else
{
return [self.KmArray count];
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.CityList objectAtIndex:row];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
if (pickerView.tag == 1) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)];
// label.backgroundColor = [UIColor grayColor];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:@"HelveticaNeue" size:20];
label.textAlignment = NSTextAlignmentCenter;
label.text = self.CityList[row];
return label;
}
else if (pickerView.tag == 2)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)];
// label.backgroundColor = [UIColor grayColor];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:@"HelveticaNeue" size:20];
label.textAlignment = NSTextAlignmentCenter;
if (row == 0) {
if ([_currentCity isEqual:@"Chennai"])
{
__weak NSString *lbl1 = [NSString stringWithFormat:@"1.8 Km"];
lbl1 = [lbl1 stringByAppendingString:@" "];
lbl1 = [lbl1 stringByAppendingString:@"25.00"];
label.text = lbl1;
return label;
}
else if ([_currentCity isEqual:@"Banglore"])
{
__weak NSString *lbl1 = [NSString stringWithFormat:@"1.9 Km"];
lbl1 = [lbl1 stringByAppendingString:@" "];
lbl1 = [lbl1 stringByAppendingString:@"25.00"];
label.text = lbl1;
return label;
}
else if ([_currentCity isEqual:@"Delhi"])
{
__weak NSString *lbl1 = [NSString stringWithFormat:@"2.0 Km"];
lbl1 = [lbl1 stringByAppendingString:@" "];
lbl1 = [lbl1 stringByAppendingString:@"25.00"];
label.text = lbl1;
return label;
}
else if ([_currentCity isEqual:@"Mumbai"])
{
__weak NSString *lbl1 = [NSString stringWithFormat:@"1.5 Km"];
lbl1 = [lbl1 stringByAppendingString:@" "];
lbl1 = [lbl1 stringByAppendingString:@"15.00"];
label.text = lbl1;
return label;
}
else if ([_currentCity isEqual:@"Puducherry"])
{
__weak NSString *lbl1 = [NSString stringWithFormat:@"1.8 Km"];
lbl1 = [lbl1 stringByAppendingString:@" "];
lbl1 = [lbl1 stringByAppendingString:@"25.00"];
label.text = lbl1;
return label;
}
else if ([_currentCity isEqual:@"Pune"])
{
__weak NSString *lbl1 = [NSString stringWithFormat:@"1.5 Km"];
lbl1 = [lbl1 stringByAppendingString:@" "];
lbl1 = [lbl1 stringByAppendingString:@"17.00"];
label.text = lbl1;
return label;
}
}
else
{
__weak NSString *lbl1 = [NSString stringWithFormat:@"%@", self.KmArray[row]];
lbl1 = [lbl1 stringByAppendingString:@" "];
lbl1 = [lbl1 stringByAppendingString:[NSString stringWithFormat:@"%@", self.fareArray[row]]];
label.text = lbl1;
return label;
}
}
return 0;
}
- (IBAction)GetFare:(id)sender
{
[self.ScrollView setAlpha:1];
NSInteger row = [_SelectCity selectedRowInComponent:0];
self.currentCity = [[NSString alloc] initWithFormat:@"%@", [_CityList objectAtIndex:row]];
NSLog(@"Selected city is %@", _currentCity);
[self populatePickerArray];
[_FareList reloadAllComponents];
}
-(void)populatePickerArray
{
float z=0.0, x = 0.0, ch = 25.0, mu = 15, pu = 17;
float y = 0.0, chd = 1.9, del = 2.1, mud = 1.6, blore = 2.0, pud = 1.6, pudd = 1.9;
if ([_currentCity isEqual:@"Chennai"])
{
x = ch;
y = chd;
z = 1.2;
[self.FareList reloadAllComponents];
}
else if ([_currentCity isEqual:@"Banglore"])
{
x = ch ;
y = blore;
z = 1.3;
[self.FareList reloadAllComponents];
}
else if ([_currentCity isEqual:@"Delhi"])
{
x = ch ;
y = del;
z = 0.8;
[self.FareList reloadAllComponents];
}
else if ([_currentCity isEqual:@"Mumbai"])
{
x = mu ;
y = mud;
z = 1.0;
[self.FareList reloadAllComponents];
}
else if ([_currentCity isEqual:@"Pune"])
{
x = pu ;
y = pud;
z = 1.16;
[self.FareList reloadAllComponents];
}
else
{
x = ch;
y = pudd;
z = 1.5;
[self.FareList reloadAllComponents];
}
float i = x;
for (float weight = y; weight <= 25.01; weight = weight + 0.1)
{
i = i + z;
//[_KmArray addObject:[NSNumber numberWithFloat:weight]];
NSString *weightString = [NSString stringWithFormat:@"%0.1f Km", weight];
[_KmArray addObject:weightString];
NSString *fareString = [NSString stringWithFormat:@"%0.2f", i];
[_fareArray addObject:fareString];
}
}
答案 0 :(得分:1)
确保调用populatePickerArray,然后在 选择器视图上调用reloadAllComponents
后立即调用。
除此之外,您的代码是混乱和冗余的。您应该考虑为选择器视图创建一个单独的对象,以使代码更具可读性。
请注意,约定是变量不以大写字母开头。因此,cityList
,而不是CityList
。大写首字母是为类和一些常量保留的。