浏览UITableView中的文本字段

时间:2015-12-24 09:27:04

标签: ios objective-c uitableview

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

提前致谢。

4 个答案:

答案 0 :(得分:1)

试试这个

我认为您应该尝试TPKeyboard来处理ids = Model.select{|s| s.age_range.include?(19)}.map(%:id) relation = Model.where(id: ids) 中的UITextfields

希望它有所帮助。

答案 1 :(得分:0)

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

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

    if (textField == <textField1>) {
        [<textField2> becomeFirstResponder];
    }
    else if (textField == <textField2>) {
        [<textField3> becomeFirstResponder];
    }
    else {

    }
    return YES;
}

答案 2 :(得分:0)

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

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

同样在返回文本字段时,您可以导航

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

答案 3 :(得分:0)

首先将视图场景的类更改为storyboard中的viewcontroller 首先制作标识为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];
    }
}

并为cell编写初始化代码:

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