UIPicker - selectRow:12 inComponent:0 animated:NO not working(12或12以上)

时间:2014-01-15 08:05:49

标签: ios objective-c uipickerview uipicker

我正面临UIPicker的一个奇怪问题。

我有UIPicker我正在向美国展示一些州。默认情况下,已完成随机选择。为此,我使用以下声明。

[statesPicker selectRow:myRegPos inComponent:0 animated:NO];

但是,此声明仅在myRegPos <= 11

时有效

当我有myRegPos >= 12时,默认的第一个选项正在被选中。知道为什么会这样吗?

何时myRegPos = 2

enter image description here

何时myRegPos >= 12

enter image description here

要确认我们有多少行,我在

中看到
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

这里我打印返回的数字,它是64。

知道为什么会这样吗?


对于标题我使用

if ([tappedWhat isEqualToString:@"states"]) {
        return [NSString stringWithFormat:@"%@", [[feedsStates objectAtIndex:row] objectForKey:[NSString stringWithFormat:@"NameS"]]];
}

我关心的是

当它与

一起使用时

[statesPicker selectRow:3 inComponent:0 animated:NO];

为什么不用

[statesPicker selectRow:12 inComponent:0 animated:NO];

NOOOOOOO .....这真让我疯狂。什么出了什么问题?这是2天,但没有解决方案:(

2 个答案:

答案 0 :(得分:0)

我假设你确保你的视图控制器同时实现UIPickerViewDataSource和UIPickerViewDelegate协议。您在 pickerView:numberOfRowsInComponent:

中定义了64行

您是如何实施 titleForRow 方法的?你把所有64个头衔都放了?或者确实只实施了12个州?

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

答案 1 :(得分:0)

在选择第12行之前调用[statesPicker reloadAllComponents] ;并确保feedsStates填充数据。

NSAssert([[NSThread currentThread] isMainThread], @"error") ;
NSAssert(statesPicker != nil, @"error") ;
NSAssert([feedsStates count] >= 12, @"error") ;
[statesPicker  reloadAllComponents] ;
[statesPicker selectRow:12 inComponent:0 animated:NO] ;
相关问题