我有一个表格视图,其中包含大量用于创建表单的单元格。我已经创建了一个自定义的TableViewCell。此单元格包含一个标签和一个文本字段,因此,例如,每行将如下所示
我也有一个自定义的表格单元,其中包含一个标签和一个文本视图,看起来与上面的图像相同,只是单元格更大,以便为文本视图留出空间。
现在,这些文本字段和文本视图中的每一个都添加了工具栏,因此当文本字段成为first responder时,将显示一个工具栏,其中包含按钮、完成、上一步和下一步按钮。
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行的一小部分。我应该补充说,我还向文本字段委托添加了代码,以便当文本字段成为第一响应者时,屏幕滚动以允许文本字段完全显示。有关代码,请参阅我以前的帖子here。
因此,当屏幕加载时,我按下第一个单元格,然后单击下一步,工作正常,移动到下一个单元格中的第二个文本字段。然后我再次点击next按钮,它转到第三个单元格,然后是第四个,然后是第五个。现在,问题就出现了。当我在第5个单元格中,单击下一步时,第6个文本域成为第一个响应者,但由于某种原因,屏幕无法滚动。现在更大的问题是,当我再次单击next时,它没有移动到第7个文本字段,因为第7个单元格还没有在内存中,因为它在屏幕上不可见。因此,如果我点击下一步,它将不会做任何事情,文本字段6将仍然是第一个响应者。因此,我需要先向下滚动一点,这样单元格7才可见,然后next按钮才会起作用,并使next文本字段成为第一响应者。
当点击上一个按钮时也会出现同样的问题,如果点击了上一个按钮,而上一个单元格不在屏幕上,那么它将不会做任何事情,直到那个单元格在屏幕上可见。
这让我很头疼,因为我似乎想不出解决这个问题的办法。还有其他人遇到过类似的问题吗?有没有好的办法来解决这个问题?
提前感谢
编辑:
我还应该加上这一点,这使得保存数据成为一个问题,因为假设我有一个按钮,当单击该按钮时,它将循环遍历每个表格单元格并保存每个字段中的数据,它将只循环遍历屏幕上可见的表格单元格,而忽略其余的。
发布于 2013-02-25 10:32:48
这对我来说很有效,我使用的是当按下键盘上的“完成”按钮将焦点移到下一个文本字段单元格时触发的块:
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;
BlocksTypedefs:
/* 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代码:
.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
发布于 2013-02-25 10:02:34
CGRect frame = CGRectMake(0.0, self.view.bounds.size.height, self.view.bounds.size.width, 44.0);
fieldAccessoryView = [[UIToolbar alloc] initWithFrame:frame];
fieldAccessoryView.barStyle = UIBarStyleBlackOpaque;
fieldAccessoryView.tag = 200;
[fieldAccessoryView setBarStyle:UIBarStyleBlack];
UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(@"Previous", @""), NSLocalizedString(@"Next", @""), nil]];
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl setMomentary:YES];
UIBarButtonItem *segmentButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[fieldAccessoryView setItems:[NSArray arrayWithObjects:segmentButton, spaceButton, doneButton, nil] animated:NO];
[segmentButton release];
[spaceButton release];
[doneButton release];
[segmentedControl release];
https://stackoverflow.com/questions/15063798
复制相似问题