UITextField becomeFirstResponder 在单元格之间传输值和属性

Posted

技术标签:

【中文标题】UITextField becomeFirstResponder 在单元格之间传输值和属性【英文标题】:UITextField becomeFirstResponder transfers values and properties between cells 【发布时间】:2014-01-10 20:35:59 【问题描述】:

所以我有一个显然无处不在的设置,即在我自己的 UITableViewCell 的自定义子类中拥有一个 UITextField。我继续使用情节提要创建了一个带有标题和文本字段的原型单元格。当您点击单元格上的任意位置时,我想专注于 UITextField。

我对 ios 很陌生,但我已经在这里解决了足够多的类似问题,以获得 几乎 工作的东西。单元格看起来与我想要的完全一样,如果您点击单元格,它会将焦点设置在文本字段上并拉起键盘。但是,如果我在文本字段中输入内容,然后点击活动文本字段外的单元格,文本就会消失。如果我然后再次点击,或在另一个单元格上,它会在点击单元格的文本字段中显示原始/消失的文本。更奇怪的是,如果原始单元格的 'isSecure' 属性设置为 YES,那么第二个单元格中显示的文本会显示为黑点,即使它最初没有设置为安全。但是,键盘类型不会改变。

如果我直接点击文本字段,它们的行为完全符合我的预期,没有文本清除、没有内容传输和显示属性传输。

当我点击包含文本字段的单元格时,我希望文本不清晰,我特别不想转移安全显示,但更根本的是,我想了解为什么它的行为如此奇怪.

我认为相关的代码:

查看控制器方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    static NSString *CellIdentifier = @"textFieldCell";
    REGTextFieldTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    REGTextFieldParameters *parameters =[self.parameterList objectAtIndex:indexPath.row];
    [cell loadFormContent:parameters];  // Set cell label, placeholder text, keyboard type
    return cell;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    [[(REGTextFieldTableCell *)[self.tableView cellForRowAtIndexPath:indexPath] textField] becomeFirstResponder];

REGTextFieldTableCell.h:

#import "REGFormTableCell.h"
#import "REGTextFieldParameters.h"

@interface REGTextFieldTableCell : REGFormTableCell

@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel *label;

- (void)loadFormContent:(REGTextFieldParameters *) params;

@end

REGTextFieldTableCell相关方法:

- (void)loadFormContent:(REGTextFieldParameters *) params

    self.label.text = params.title;
    self.textField.placeholder = params.title;
    self.textField.translatesAutoresizingMaskIntoConstraints = NO;
    if([params.keyboardType isEqual:@"email"])
    
        self.textField.keyboardType = UIKeyboardTypeEmailAddress;
        self.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    else
        self.textField.keyboardType = UIKeyboardTypeDefault;
        self.textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
    
    if(params.isSecure)
        self.textField.secureTextEntry = YES;
        self.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    

对不起,如果这是一个骗局,我已经找了几个小时,但没有找到任何与此类似的东西。如果您在复制时遇到问题,还请告诉我应该包含哪些其他代码。提前感谢您的帮助。

【问题讨论】:

【参考方案1】:

好的,所以我想出了如何解决它,尽管老实说我不确定它为什么会起作用:

我注释掉了这一行:

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

我不确定为什么在重新加载行时会出现问题,但至少我有我的解决方案。

【讨论】:

以上是关于UITextField becomeFirstResponder 在单元格之间传输值和属性的主要内容,如果未能解决你的问题,请参考以下文章

如何在 UITextField 子类中设置 UITextField 的属性

UITextField

在 UITextField 操作中将 UITextField 字符串转换为 Double

如何调整 UITextField 占位符文本以适合 UITextField 宽度?

如果添加了新的 UITextField,如何向下移动许多 UITextField

如果 UITextField 已经创建,不要在 Tap 上重新创建 UITextField