2017-06-12 66 views
0

我在項目的不同部分實現了多個UIPickerViews。大多數工作正常。然而,最新的有一些很難描述的非常奇怪的行爲,但你可以在video I've uploaded to HERE中看到它。簡而言之,有2個異常:UIPickerView行在滾動期間消失

  1. UIPickerView第一次顯示時,它是空的,並且行看起來幾乎爲零。如果我再次單擊動作按鈕(使拾取器視圖再次出現),它會正常顯示。 (更新:當我註釋掉pickerView:rowHeightForComponent:方法時,不會發生此問題)。
  2. 然後更奇怪的部分是滾動選取器視圖導致物品消失時,他們滾動(和滾動回來的另一種方式不會使他們重新出現)。

該實現相當簡單,與我在其他地方所做的類似,所以我無法弄清楚我做錯了什麼。數據源使用簡單的數組並且日誌記錄顯示它正在正常工作。即使屏幕上沒有顯示任何值的選取器的初始顯示,也會根據NSLog行正確記錄這些值。事實上,它與第二次調用的記錄完全相同,在該調用中它正確顯示值(在滾動使它們消失之前)。

「庫」欄按鈕將調用下面的行動,以配置爲UITextField與UIPickerView,因爲它的輸入,然後使該文本字段第一響應:

- (IBAction)selectStyleFromLibrary:(id)sender { 
    _dummyPickerField = [[UITextField alloc] init]; 
    [self.view insertSubview:_dummyPickerField atIndex:0]; 

    StylesController *stylesLibrary = [[StylesController alloc] init]; 
    [stylesLibrary getStyles]; 

    UIPickerView *libraryPicker = [[UIPickerView alloc] init]; 
    libraryPicker.delegate = stylesLibrary; 
    libraryPicker.dataSource = stylesLibrary; 

    _dummyPickerField.inputView = libraryPicker; 
    [_dummyPickerField becomeFirstResponder]; 
} 

所有選擇器視圖的委託和數據實施的源方法是:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 
    return 1; 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
    return styleKeys.count; 
} 

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { 
    return 64; 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 
    NSLog(@"%@", styleKeys[row]); 
    return styleKeys[row]; 
} 

任何想法,爲什麼這不正常表現?

更新:我也嘗試了一個簡單的現有文本字段,而不是在飛行中創建一個。它沒有幫助。

+0

移動這條線按鈕點擊動作之外,使libraryPicker一個全局變量UIPickerView * libraryPicker = [[UIPickerView的alloc]在裏面];如果您發佈完整的.m文件,那麼我可以對其進行更正! – dip

+0

猜猜,拾取器實例沒有正確渲染,只是讓全局並嘗試,我相信這將解決您的問題。 – dip

+0

試過了,但不幸的是,它沒有幫助。無論如何感謝您的建議。 –

回答

0

這裏是我如何解決它(從@dip評論有點啓發)...

我的數據源/委託對象是一個單獨的對象,而且它沒有被明確地保留在任何地方,所以我有現在重新安排了一些東西,使它成爲一個屬性,並將其分配到viewDidLoad中,以便它繼續存在。

我猜UIPickerView只有一個弱引用(或兩個弱引用),這當然不足以讓它長時間提供數據和委託。

所以現在我有以下(不包括實際代表團和數據源的方法):

@property (nonatomic, strong) StylesController *stylesLibrary; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Metal Tile Very Light"]]; 
    self.editor = (StyleEditor *)self.tabBarController; 

    if (! self.editor.namedStyle) { 
     nameField.borderStyle = UITextBorderStyleNone; 
     nameField.userInteractionEnabled = NO; 
    } 

    _stylesLibrary = [[StylesController alloc] init]; 
    [_stylesLibrary getStyles]; 
} 

- (IBAction)selectStyleFromLibrary:(id)sender { 
    _dummyPickerField = [[UITextField alloc] init]; 
    [self.view insertSubview:_dummyPickerField atIndex:0]; 

    UIPickerView *libraryPicker = [[UIPickerView alloc] init]; 
    libraryPicker.delegate = _stylesLibrary; 
    libraryPicker.dataSource = _stylesLibrary; 

    _dummyPickerField.inputView = libraryPicker; 
    _dummyPickerField.inputAccessoryView = [self accessoryViewWithText:@"Done" target:self action:@selector(libraryPickerDone) forField:_dummyPickerField]; 
    [_dummyPickerField becomeFirstResponder]; 
}