带有 TextFields 的 iPhone TableView - 下一个,上一个按钮问题 [英] iPhone TableView with TextFields - next, previous button issue

查看:36
本文介绍了带有 TextFields 的 iPhone TableView - 下一个,上一个按钮问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格视图,其中包含大量用于创建表单的单元格.我创建了一个自定义的 TableViewCell.这个单元格包含一个标签和一个文本字段,例如,每一行看起来像这样

I have a table view which has a good number of cells in it used to create a form. I have created a custom TableViewCell. This cell contains a label and a text field, so for example, each row will look something like this

我还有一个自定义表格单元格,其中包含一个标签和一个文本视图,看起来与上图相同,只是单元格更大,以便为文本视图留出空间.

I also have a custom table cell which contains a label and a text view, looks the same as image above, just cell is larger to allow room for the textview.

现在每个文本字段和文本视图都添加了一个工具栏,因此当文本字段成为第一响应者时,工具栏会显示按钮、完成、上一个和下一个按钮.

Now each of these text fields and text views has a tool bar added, so when text field becomes first responder, a tool bar is shown with the buttons, done, previous and next buttons.

  UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithTextField:)];

        UIBarButtonItem *nextButton = [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStyleDone target:self action:@selector(nextTableTextField:)];

        UIBarButtonItem *previousButton = [[UIBarButtonItem alloc]initWithTitle:@"Previous" style:UIBarButtonItemStyleDone target:self action:@selector(previousTableTextField:)];


        UIToolbar *numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
        numberToolbar.barStyle = UIBarStyleBlackTranslucent;
        numberToolbar.items = [NSArray arrayWithObjects:
                               previousButton,
                               nextButton,
                               [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                               doneButton,
                               nil];

        [numberToolbar sizeToFit];

        field.inputAccessoryView = numberToolbar;

我遇到的问题是,假设我有 10 个这样的表格行,屏幕只能显示大约 5 个完整的行和第 6 行的一点.我应该补充一点,我还向文本字段委托添加了代码,以便当文本字段成为第一响应者时,屏幕滚动以允许完全显示文本字段.请参阅我的旧帖子此处 代码.

The issue I am having is, say I have 10 table rows like this, the screen can only show about 5 full rows and a bit of the 6th row. I should add I also have added the code to the text field delegates so that when a text field becomes first responder, screen scrolls to allow the text field to be fully shown. See my older post here for code.

所以当屏幕加载时,我按下第一个单元格,然后单击下一步,工作正常,移动到下一个单元格中的第二个文本字段.然后我再次按下下一个按钮,它转到第 3 个单元格,然后是第 4 个,然后是第 5 个.现在这个问题出现了.当我在第 5 个单元格上单击下一步时,第 6 个文本字段将成为第一响应者,但由于某种原因屏幕不滚动.现在更大的问题是,当我再次单击下一步时,它不会移动到第 7 个文本字段,因为第 7 个单元格还没有在内存中,因为它在屏幕上不可见.所以如果我点击下一步,它不会做任何事情,文本字段 6 将保留为第一响应者.所以我需要先向下滚动一点,以便在下一个按钮起作用并使下一个文本字段成为第一响应者之前可以看到单元格 7.

So when screen loads I press the first cell, and click next, works fine, moves onto the 2nd text field in the next cell. Then I hit the next button again, and it goes to the 3rd cell, then 4th, then 5th. Now at this point the problem occurs. When I am on the 5th cell, and I click next, the 6th text field becomes the first responder but the screen does not scroll for some reason. Now the bigger problem is, when I click next again, it does not move onto the 7th text field, as the 7th cell is not in memory yet as it is not visible on the screen. So if I hit next, it won't do anything, text field 6 will remain as the first responder. So I need to scroll down a bit first, so cell 7 is visible before the next button will work and make the next text field the first responder.

点击上一个按钮时也是同样的问题,如果点击上一个按钮并且前一个单元格不在屏幕中,那么在该单元格在屏幕上可见之前它不会做任何事情.

It is the same issue when hitting the previous button, if hit previous button and the previous cell is not in screen, then it will not do anything until that cell is visible on the screen.

这让我很头疼,因为似乎无法找到解决方法.有没有其他人遇到过类似的问题?这个问题有什么好的解决方法吗?

This has been causing me quite a headache as can't seem to figure out a way around this. Has anyone else had similar problems like this? Is there a good work around to this problem?

提前致谢

我还应该添加这使得保存数据成为一个问题,因为假设我有一个按钮,当单击该按钮时,循环遍历每个表格单元格并将数据保存在每个字段中,它只会遍历屏幕上可见的表格单元格并忽略剩下的.

I should also add this makes saving data a problem because say I have a button that when clicked loops through each table cell and saves the data in each field, it will only loop through the table cells that are visible on screen and ignore the rest.

推荐答案

它对我有用,我使用的是当按下键盘上的完成"按钮将焦点移到下一个文本字段单元格时触发的块:

It works for me, I'm using blocks which is fired when "done" button on keyboard pressed to move focus into next text fielded cell:

FRTextFieldTableViewCell *textFieldCell = [tableView_ dequeueReusableCellWithIdentifier:[FRTextFieldTableViewCell reuseIdentifier]];
        if(textFieldCell == nil) {
            textFieldCell = [[FRTextFieldTableViewCell alloc] initWithStyle:
                             UITableViewCellStyleDefault reuseIdentifier:[FRTextFieldTableViewCell reuseIdentifier]];
        }

        [textFieldCell.textField initialize];
        textFieldCell.textField.secureTextEntry = (indexPath.row == 3);

        __weak FRSignUpViewController *self_ = self;
        __weak FRTextFieldTableViewCell *textFieldCell_ = textFieldCell;
        if(indexPath.row == 1) {
            textFieldCell.textField.placeholder = NSLocalizedString(@"name", @"");
            textFieldCell.object = self.regUser.name;
            textFieldCell.didChangedValueAction = ^(NSString *object) {
                [self_ setName:object];
            };
            textFieldCell.didEndOnExitAction = ^{
                [self_ setName:textFieldCell_.object];
                FRTextFieldTableViewCell *nextCell = (FRTextFieldTableViewCell *)[self_.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
                [nextCell.textField becomeFirstResponder];
            };
        }
        if(indexPath.row == 2) {
            textFieldCell.textField.placeholder = NSLocalizedString(@"email", @"");
            textFieldCell.didChangedValueAction = ^(NSString *object) {
                [self_ setLoginString:object];
            };
            textFieldCell.didEndOnExitAction = ^{
                [self_ setLoginString:textFieldCell_.object];
                FRTextFieldTableViewCell *nextCell = (FRTextFieldTableViewCell *)[self_.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
                [nextCell.textField becomeFirstResponder];
            };
        }
        if(indexPath.row == 3) {
            textFieldCell.textField.placeholder = NSLocalizedString(@"password", @"");
            textFieldCell.didChangedValueAction = ^(NSString *object) {
                [self_ setPasswordString:object];
            };
            textFieldCell.didEndOnExitAction = ^{
                [self_ setPasswordString:textFieldCell_.object];
                [self_ signUp];
            };
        }
        [textFieldCell reloadData];
        return textFieldCell;

块类型定义:

/* type defenition for blocks, can be used by any app class */
typedef void (^CompletionBlock)(id, NSError *);
typedef void (^SimpleBlock)(void);
typedef void (^InfoBlock)(id);
typedef void (^ConfirmationBlock)(BOOL);

TableViewCell 代码:

TableViewCell code:

.h 文件:

#import "FRTableViewCell.h"
#import "BlocksTypedefs.h"
#import "FRTextFieldWithPadding.h"

@interface FRTextFieldTableViewCell : FRTableViewCell <UITextFieldDelegate>

@property (nonatomic, copy) SimpleBlock didEndOnExitAction;
@property (nonatomic, copy) SimpleBlock didEndEditingAction;
@property (nonatomic, copy) InfoBlock didChangedValueAction;
@property (nonatomic, strong) IBOutlet FRTextFieldWithPadding *textField;

@end

.m:

    #import "FRTextFieldTableViewCell.h"

@implementation FRTextFieldTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.backgroundColor = [UIColor clearColor];
}

- (void)reloadData {
    self.textField.text = self.object;
}

- (IBAction)textFieldValueDidChanged:(UITextField *)sender {
    self.object = sender.text;
    if (self.didChangedValueAction) {
        self.didChangedValueAction(self.object);
    }
}

- (IBAction)textFieldDidEndOnExit:(UITextField *)sender {
    self.object = sender.text;
    if (self.didEndOnExitAction) {
        self.didEndOnExitAction();
    }
}

#pragma mark - UITextFieldDelegate

- (void)textFieldDidEndEditing:(UITextField *)textField {
    self.object = textField.text;
    if (self.didEndEditingAction) {
        self.didEndEditingAction();
    }
}

@end

这篇关于带有 TextFields 的 iPhone TableView - 下一个,上一个按钮问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆