UIPickerView 的 selectedRowInComponent 仅在旋转且未选中时返回随机行。怎么了?
Posted
技术标签:
【中文标题】UIPickerView 的 selectedRowInComponent 仅在旋转且未选中时返回随机行。怎么了?【英文标题】:selectedRowInComponent for UIPickerView is returning random rows only when spun and not selected. What is happening? 【发布时间】:2013-03-15 02:26:16 【问题描述】:我有一个双选择器和两个 NSInteger 变量,用于更新选择器的每个组件所在的行。在 iPhone 模拟器中(我没有在实际的 iPhone 上测试过),如果我选择行(即单击下方或上方),数字很好,但是当我旋转时,我收到的行号通常与实际相差几个。这是我所拥有的:
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
if (component == kSuitComponent)
UIImage *image = self.suitImages[row];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self theSuitRow];
return imageView;
else
NSString *text = self.cardNumber[row];
UILabel *label = [[UILabel alloc] init];
[self getCardRow];
label.text = text;
return label;
-(void) getCardRow
cardRow = [_doublePicker selectedRowInComponent:kCardNumberComponent];
-(void) theSuitRow
suitRow = [_doublePicker selectedRowInComponent:kSuitComponent];
【问题讨论】:
viewForRow:forComponent: 用于用视图填充行,并被多次调用——您不应该使用它来获取选定的行。您是否有理由不使用 pickerView:didSelectRow:inComponent: 【参考方案1】:应该是:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
if (component == kSuitComponent)
UIImage *image = self.suitImages[row];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
return imageView;
else
NSString *text = self.cardNumber[row];
UILabel *label = [[UILabel alloc] init];
label.text = text;
return label;
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
if (component == kCardNumberComponent)
cardRow = row;
else
suitRow = row;
【讨论】:
以上是关于UIPickerView 的 selectedRowInComponent 仅在旋转且未选中时返回随机行。怎么了?的主要内容,如果未能解决你的问题,请参考以下文章
如何从另一个 UIPickerview 更改 UIPickerView 的字符串?