UIPickerView 没有重新加载
Posted
技术标签:
【中文标题】UIPickerView 没有重新加载【英文标题】:UIPickerView Not getting reloaded 【发布时间】:2014-01-14 13:03:24 【问题描述】:我在我的示例应用程序中使用了 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];
【问题讨论】:
【参考方案1】:确保在 both 您的选择器视图上调用了 populatePickerArray 并在调用 reloadAllComponents
之后立即调用。
除此之外,您的代码是混乱和冗余的。您应该考虑为您的选择器视图创建一个单独的对象,以使代码更具可读性。
请注意,约定是变量不以大写字母开头。因此,cityList
,而不是CityList
。大写首字母为类和一些常量保留。
【讨论】:
第一个 pickerView 一切正常……问题仅出在第二个 pickerView 上,感谢您对命名约定和代码可读性的更正……即使调用了 reloadAllComponents立即它的讲解员工作...... :( 这意味着您还没有更新负责第二个选择器视图的数组。设置断点并检查。以上是关于UIPickerView 没有重新加载的主要内容,如果未能解决你的问题,请参考以下文章
UIPickerView 未使用最新的 JSON 数据重新加载
UIPickerView 中的 UILabel 上的额外空间