UIPickerView第二个组件在第二次显示选择器视图时填充空白

时间:2015-12-18 18:19:40

标签: ios objective-c uipickerview

我有一个带有2个组件的UIPickerView。选择第一个组件将填充第二个组件。看起来像这样:

enter image description here

测试用例:

  1. 选择Component 0中的任意选项,然后选择Component 1选择器中的任何一集,然后点击隐藏选择器的Fitler
  2. 再次显示选择器以选择其他内容。第一个组件正确加载。第二个组件是空白的,如截图:
  3. enter image description here

    以下是代码:

    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
        UILabel* tView = (UILabel*)view;
        if (!tView){
            tView = [[UILabel alloc] init];
            tView.textAlignment = NSTextAlignmentCenter;
            // Setup label properties - frame, font, colors etc
            //...
            //adjustsFontSizeToFitWidth property to YES
    
            if(pickerView == self.seasonEpiPickerView && component == 1){
                tView.font= [UIFont systemFontOfSize:12];
                tView.adjustsFontSizeToFitWidth = YES;
            }
        }
    
        // Fill the label text here
         if (pickerView == self.seasonEpiPickerView)
        {
            if (component == 0) {
                if (row == 0) {
                    item = @"All";
                } else {
                    //NSLog(@"c1:%ld", (long)row);
                    item = [NSString stringWithFormat:@"S%@", ((Season *)[self.media.seasonManager.seasonArray objectAtIndex:row-1]).seasonNr];
                }
    
            } else if (component == 1){
                if(self.media.seasonManager && self.media.seasonManager.seasonArray && [self.media.seasonManager.seasonArray count]>0 ) {
                    Season* tempSeason = self.media.seasonManager.seasonArray[[self.seasonEpiPickerView selectedRowInComponent:0]-1];
                    item = [NSString stringWithFormat:@"%@.%@ \"%@\"", tempSeason.seasonNr, [NSString stringWithFormat: @"%02d", [((Episode *)[tempSeason.episodeManager.episodeArray objectAtIndex:row]).episodeNr intValue]], ((Episode *)[tempSeason.episodeManager.episodeArray objectAtIndex:row]).title];
                    NSLog(@"c2:%ld:%@", (long)row, item);
                }
            }
        }
        tView.text = item;
    
        return tView;
    }
    

    修改

    以下是我在viewDidLoad

    中创建Picker的方法
    self.seasonPickerViewTextField  = [[UITextField alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.seasonPickerViewTextField];
    
    self.seasonEpiPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    self.seasonEpiPickerView.showsSelectionIndicator = YES;
    self.seasonEpiPickerView.dataSource = self;
    self.seasonEpiPickerView.delegate = self;
    
    //add the toolbar for the season picker
    UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,screenWidth,44)];
    [toolBar setBarStyle:UIBarStyleBlack];
    UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Filter   " style:UIBarButtonItemStylePlain target:self action:@selector(filterByEpisode:)];
    UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *barCancelButton = [[UIBarButtonItem alloc] initWithTitle:@"   Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelPicker:)];
    toolBar.items = [NSArray arrayWithObjects: barCancelButton, flexible, barButtonDone, nil];
    self.seasonPickerViewTextField.inputAccessoryView = toolBar;
    
    // set change the inputView (default is keyboard) to UIPickerView
    self.seasonPickerViewTextField.inputView = self.seasonEpiPickerView;
    

    编辑2 在呈现选择器视图时执行延迟重新加载,第二次正确地重新加载第二个组件。这似乎非常强硬。

    - (IBAction)pickEpisodeButton:(id)sender
    {
        [self.seasonPickerViewTextField becomeFirstResponder];
    
        /* None of the following commented lines of code have any effect*/
        //[self.seasonEpiPickerView selectRow:1 inComponent:0 animated:NO];
        //[self.seasonEpiPickerView selectRow:selectedSeason inComponent:0 animated:YES];
        //[self.seasonEpiPickerView selectRow:selectedEpisode inComponent:1 animated:YES];
        //[self.seasonEpiPickerView reloadComponent:1];
    
        //set a 1.5 sec timer to go to the next screen
        [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(loadSecondComponent) userInfo:nil repeats:NO];
    }
    
    
    -(void)loadSecondComponent {
        [self.seasonEpiPickerView reloadComponent:1];
    }
    

0 个答案:

没有答案