【问题标题】:Navigate through textfields in the UITableView浏览 UITableView 中的文本字段
【发布时间】:2019-02-28 22:14:43
【问题描述】:

我在TableView 中使用自定义单元格。我的 Tableview 有 5 行,我重复使用单元格 5 次,单元格有 Textfield。我需要浏览表格视图中的文本字段并按下键盘上的下一个按钮。 请帮帮我。

提前致谢。

【问题讨论】:

  • 当你按下下一个按钮时你真正想做的动作是什么?
  • 按下下一个按钮时,我需要转到表格视图中的下一个文本字段
  • 为单元格中的每个 uitextfield 提供标签。按下下一个按钮使另一个文本字段成为FirstResponder
  • 它很痛苦的朋友。
  • 我知道,但我使用的是同一个单元格,从技术上讲它包含相同的文本字段,所以我怎样才能将标签赋予文本字段。

标签: ios objective-c uitableview


【解决方案1】:

试试这个

我认为您应该尝试使用TPKeyboardUITableView 中处理UITextfields

希望对你有帮助。

【讨论】:

    【解决方案2】:

    首先将视图场景的类更改为情节提要中的视图控制器。
    首先使用标识符cell(在我的情况下)创建您的自定义单元格。
    放置文本字段并设置标签101(在我的情况下)。
    像这样继承 UITextFieldDelegate 和键盘工具栏值:

    @interface TableViewController () <UITextFieldDelegate>
    @property (nonatomic, strong) UITextField* textField1;
    @property (nonatomic, strong) UITextField* textField2;
    @property (nonatomic, strong) UITextField* lastTextField;
    @property (nonatomic, copy) NSString* string1;
    @property (nonatomic, copy) NSString* string2;
    @property (nonatomic, strong) UIToolbar* keyboardToolbar;
    @end
    

    viewDidLoad方法下面写代码:

    - (void)viewDidLoad {
        [super viewDidLoad];
        //
        self.keyboardToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
        self.keyboardToolbar.barStyle = UIBarStyleBlackTranslucent;
        self.keyboardToolbar.items = @[[[UIBarButtonItem alloc]initWithTitle:@"Cancel"
                                                                       style:UIBarButtonItemStylePlain
                                                                      target:self
                                                                      action:@selector(cancelNumberPad:)],
        [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                     target:nil
                                                     action:nil],
        [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone
                                                      target:self
                                                      action:@selector(doneWithNumberPad:)]];
        [self.keyboardToolbar sizeToFit];
    }
    

    并实现这些方法:

    -(void)cancelNumberPad:(id)sender {
        [self.lastTextField resignFirstResponder];
    }
    
    -(void)doneWithNumberPad:(id)sender {
        if(self.lastTextField == self.textField1) {
            [self.textField2 becomeFirstResponder];
        } else {
            [self.lastTextField resignFirstResponder];
        }
    }
    

    并为单元格编写初始化代码:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
        UITextField* textField = [cell viewWithTag:101];
        textField.inputAccessoryView = self.keyboardToolbar;
        textField.delegate = self;
    
        if(0 == indexPath.section && 0 == indexPath.row) {
            self.textField1 = textField;
        } else if(0 == indexPath.section && 1 == indexPath.row) {
            self.textField2 = textField;
        }
    
        return cell;
    }
    

    现在我们必须实现 UITextFieldDelegate 方法:

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        if(textField == self.textField1) {
            self.string1 = [self.string1 stringByReplacingCharactersInRange:range withString:string];
        } else if(textField == self.textField2) {
            self.string2 = [self.string2 stringByReplacingCharactersInRange:range withString:string];
        }
        return YES;
    }
    
    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        self.lastTextField = textField;
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [self doneWithNumberPad:nil];
        return YES;
    }
    

    【讨论】:

      【解决方案3】:

      在结束编辑文本字段时使用此方法,以便检查特定文本字段并导航

      - (void)textFieldDidEndEditing:(UITextField *)textField;
      

      还可以在返回文本字段时导航

      - (BOOL)textFieldShouldReturn:(UITextField *)textField;
      

      【讨论】:

        【解决方案4】:

        通常textFieldShouldReturn 方法用于此目的。下面是一个示例。您需要修改它以在您的代码中使用。您将要在其中编写此方法的类,该类的对象必须为这些文本字段设置为delegate

        - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        
            if (textField == <textField1>) {
                [<textField2> becomeFirstResponder];
            }
            else if (textField == <textField2>) {
                [<textField3> becomeFirstResponder];
            }
            else {
        
            }
            return YES;
        }
        

        【讨论】:

        • 这是不正确的。在视图内部的常见情况下,这是可能的。但是在每个单元格中,只有您可以仅根据索引路径从一个字段跳到另一个字段。
        • 是的,正确的。这就是为什么我说它只是要使用的方法的一个示例,OP 必须对其进行修改。
        • 我需要通过提供等于 indexpath.row 的标签来识别文本字段。我在文本字段中编写一个块应该返回方法,我在主控制器中调用它。
        • 是的,您可以为您的 textField 设置该标签,基于该标签您可以导航到下一个单元格中的 textField。它有点复杂,但我想这应该可行。
        猜你喜欢
        • 1970-01-01
        • 2012-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-24
        相关资源
        最近更新 更多