【问题标题】:UIPickerView rows disappear during scrollUIPickerView 行在滚动期间消失
【发布时间】:2017-06-12 11:48:23
【问题描述】:

我已经在项目的不同部分实现了几个 UIPickerView。大多数都工作得很好。然而,最新的一个有一些非常奇怪的行为,很难描述。简而言之,有2个异常:

  1. 第一次显示 UIPickerView 时,它是空的,并且行看起来几乎为零大小。如果我再次单击操作按钮(使选择器视图再次出现),它就会正常显示。 (更新:当我注释掉 pickerView:rowHeightForComponent: 方法时不会出现此问题)。
  2. 然后更奇怪的部分是滚动选择器视图会导致项目在滚动时消失(并且以其他方式向后滚动不会使它们重新出现)。

实现相当简单,与我在其他地方所做的类似,所以我无法弄清楚我做错了什么。数据源使用一个简单的数组,日志显示它工作正常。即使选择器的初始显示在屏幕上没有显示任何值,也确实按照NSLog 行正确记录了值。事实上,它的日志记录与第二次调用完全相同,在该调用中它确实正确显示了值(在滚动使它们消失之前)。

“库”栏按钮调用以下操作,以便在输入时使用 UIPickerView 配置 UITextField,然后使该文本字段成为第一响应者:

- (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];
}

任何想法为什么这不正常?

更新:我还尝试了一个简单的现有文本字段,而不是即时创建一个。它没有帮助。

【问题讨论】:

  • 将此行移到按钮单击操作之外,使 libraryPicker 成为全局变量 UIPickerView *libraryPicker = [[UIPickerView alloc] init];如果您发布整个 .m 文件,那么我可以对此进行更正!
  • 猜猜,Picker 实例没有被正确渲染,只需全局并尝试,我相信这会解决你的问题。
  • 试过了,但不幸的是,它没有帮助。无论如何感谢您的建议。
  • 你能发布你更新的代码吗?
  • 为什么不点击按钮而不是文本字段?

标签: ios objective-c uipickerview


【解决方案1】:

这是我解决它的方法(从@dip cmets 获得了一点灵感)...

我的数据源/委托对象是一个单独的对象,它没有被明确地保留在任何地方,所以我现在重新安排了一些东西,使它成为一个属性,并在 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];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多