【问题标题】:How can I elegantly make UITextField in UITableViewCell firstResponder?如何在 UITableViewCell firstResponder 中优雅地制作 UITextField?
【发布时间】:2015-07-28 19:52:51
【问题描述】:

我有一个有几行的UITableView,其中可能包含UITextFields。我为此UITableViewCells 创建了一个自定义类。这个自定义类有一个委托,如下所示:

@interface GKCustomCell : UITableViewCell <UITextFieldDelegate>
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) id<UITextFieldDelegate> delegate;
@end

我的TableViewController符合&lt;UITextFieldDelegate&gt;协议,所以我可以在-tableView:cellForRowAtIndexPath:中分配自己:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GKCustomCell *cell = (GKCustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.delegate = self;
    ...
    return cell;
}

cell的方法直接调用它的delegate对应的方法如下:

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self.delegate textFieldDidEndEditing:textField];
}

所以我已经知道如何正确访问在我的控制器的单元格中发生的UITextField 事件。

我的问题是,我想通过某种逻辑选择文本字段(使它们成为firstResponder)(例如,按下完成的下一个)。为此,我显然需要单元格中UITextField 的引用,它没有也不能有(因为行是动态出队的,所以我不能在我的表格视图控制器中将强引用放入单元格的内部对象)。

我怎样才能优雅地访问这些UITextFields 之一?

【问题讨论】:

    标签: objective-c uitableview uitextfield


    【解决方案1】:

    使用自定义块。在你的 cell.h 中定义一个块,如下所示 -

    typedef void (^textField_block_t)(UITextField * txtField);
    

    然后创建一个相同的实例如下 -

    @property (copy) textField_block_t txtFieldBlock;
    

    在 cell.m 中,无论您想将文本字段传递给 TableViewController 类的什么事件,都可以像下面这样调用该块 -

    if ([self txtFieldBlock])
    {
        [self txtFieldBlock]([self textField]);
    }
    

    在你的 TableviewController 的 cellForRowAtIndexPath 中,你需要像这样设置这个块 -

    GKCustomCell *cell = (GKCustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
     [cell setTxtFieldBlock:^(UITextField * txtField){
       //Here you will be getting your instance of textfield which belongs to that particular cell. Do whatever you want to do with it.
    
      }];
    

    【讨论】:

    • 这不是我的问题的答案。我想要 UITextFields 的参考。
    • 你明白了,txtField 是属于特定单元格的 UITextField 的引用。
    • 好吧,你是对的,但你误解了我的意思。我仍然无法随时访问该引用(在 cellForRow... 被调用后它才可用)。我没有看到这比更简单的self.textFieldReference = cell.textField...
    【解决方案2】:

    您可以先在 storyborad 或代码中设置textField.userInteractionEnabled = No,这样您就可以在 tableView 上获取事件。 然后在

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UITableViewCell *cell  = [self.tableView cellForRowAtIndexPath:indexPath];
    [cell.textField becomeFirstResponder];
    }
    

    现在您可以调用文本字段委托方法 快乐编码

    【讨论】:

    • 只有当您想访问您点击的单元格中的文本字段时才有效。如果您想访问另一个单元格中的文本字段(可能在屏幕外,因此甚至不在内存中),此方法不起作用。例如:我想跳到下一个字段 - 我该怎么做?
    • 你还有索引路径,你可以在下一个单元格中设置文本字段为第一响应者
    • 怎么样?我应该明确地调用-cellForRowAtIndexPath: 来获取单元格吗?它“合法”吗?
    • 我认为NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow]; 然后您可以将新单元格设为UITableViewCell *cell =[self.tableView cellForRowAtIndexPath:[[NSIndexPath indexPathForRow:row inSection:section]]];
    • 想象一下:我在表格中有 10 个文本字段,在 10 个单元格中。其中一些在屏幕外。如果我在第一个按完成,我想跳到第 10 个。我该怎么做?
    猜你喜欢
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 2022-10-15
    相关资源
    最近更新 更多