简体   繁体   English

在UITableView重新加载时以编程方式打开/关闭UITextField的inputView

[英]Open/close UITextField's inputView programmatically on UITableView reload

I have a UITextField that is part of a UITableViewCell , the table should be a dumb component. 我有一个UITextField ,它是UITableViewCell一部分,表应该是一个哑组件。

The events triggered by the UITextField are handled by a custom delegate, which: UITextField触发的事件由自定义委托处理,该委托:

  1. Every value change (typing, spinning the picker,...) triggers a chain of events to store and process the data 每次值更改(键入,旋转选择器等)都会触发一系列事件,以存储和处理数据
  2. At the end of the chain UITableView is fully reloaded. 在链的末尾, UITableView已完全重新加载。
  3. The full reload causes the inputView to disappear. 完全重新加载会导致inputView消失。

While I want 1. and 2., I don't want 3. The inputView should remain open and focused on the UITextField being edited -> I am looking for a way to open/close it programmatically and perhaps place it in -(void)prepareForReuse of the UITableViewCell 当我想要1.和2.时,我不需要3. inputView应该保持打开状态,并专注于正在编辑的UITextField >我正在寻找一种以编程方式打开/关闭它并可能将其放置在-(void)prepareForReuseUITableViewCell

My most generic custom UITableViewCell initialization: 我最通用的自定义UITableViewCell初始化:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.inputField = [UITextField new];
        [self.inputField addTarget:self
                  action:@selector(textFieldDidChange:)
        forControlEvents:UIControlEventEditingChanged];

        [self.contentView addSubview:self.inputField];

        [self.inputField setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self.inputField.leftAnchor constraintEqualToAnchor:self.contentView.leftAnchor].active = YES;
        [self.inputField.rightAnchor constraintEqualToAnchor:self.contentView.rightAnchor].active = YES;
        [self.inputField.topAnchor constraintEqualToAnchor:self.contentView.topAnchor].active = YES;
        [self.inputField.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor].active = YES;
    }
    return self;
}

Example of change handling ( inputView could be a standard keyboard, UIPickerView , or something custom): 变更处理的示例( inputView可以是标准键盘, UIPickerView或自定义项):

-(void) textFieldDidChange:(UITextField *)textField {
    NSString *valueString = textField.text;

    [self.delegate handleValueChange:valueString forAttributeID:self.attributeID];
}

I think the problem is that once you call reloadData , new cells will be created for all visible sections/rows, so the original input view loses its first responder state. 我认为问题在于,一旦调用reloadData ,就会为所有可见的部分/行创建新的单元格,因此原始输入视图将失去其第一个响应者状态。

There are many ways you could circumvent this. 您可以通过多种方法来规避这一点。 Maybe the most easy one is to not call reloadData , but instead just reload the cells that are "changed" (´reloadRowsAtIndexPaths:withRowAnimation:`) - and especially not to reload the cell with the inputView that caused the change 也许最简单的方法是不调用reloadData ,而是重新加载“已更改”的单元格(´reloadRowsAtIndexPaths:withRowAnimation:`)-特别是不要使用导致更改的inputView重新加载单元格

Or, you could store the "active" IndexPath, and after refresh let the appropriate inputView become first responder again. 或者,您可以存储“活动的” IndexPath,并在刷新后让适当的inputView再次成为第一响应者。

There might be other solutions to think of - all of them depend on your workflow. 可能还需要考虑其他解决方案-所有这些都取决于您的工作流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM