2013-02-25 36 views
1

我有具有相当数量的细胞在其用于创建形式的表格视图。我创建了一个自定义TableViewCell。此单元格包含一个标签和一个文本字段,因此,例如,每个行会是这个样子iPhone的TableView用的TextField - 下一个,上按钮问题

enter image description here

我也有一个包含一个标签和一个文本视图的自定义表格单元格,看起来一样上面的图片,只是单元格较大,为textview留出空间。

现在,这些文本框和文本视图中有一个工具栏添加的,所以当文本字段成为第一个响应,显示与按钮的工具栏,完成后,一个和下一个按钮。

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,做工精细,在接下来的小区中移动到第二个文本字段。然后我再次点击下一个按钮,然后进入第3格,然后是第4格,然后是第5格。现在在这一点上出现问题。当我在第5个单元格中,然后单击下一个时,第6个文本字段将成为第一个响应者,但由于某种原因,屏幕不会滚动。现在更大的问题是,当我再次单击下一个时,它不会移动到第7个文本字段,因为第7个单元格不在内存中,因为它在屏幕上不可见。因此,如果我接下来打了什么,它什么都不会做,文本字段6将保持为第一响应者。所以我需要先向下滚动一下,所以单元格7在下一个按钮可以工作之前是可见的,并且使下一个文本字段成为第一响应者。

这是同一个问题打前面的按钮时,如果击中以前按钮和以前的小区不在屏幕,然后直到细胞是在屏幕上可见它不会做任何事情。

这已造成我挺头疼的,似乎无法找出解决的办法。有没有其他人有过类似的问题?有没有很好的解决这个问题?

在此先感谢

编辑:

我还要补充,这使得数据保存问题,因为说我有一个按钮,点击时通过每个表格单元的循环和每个字段保存的数据,只会循环显示在屏幕上的表格单元格,而忽略其余部分。

回答

1

它的工作对我来说,我使用的时候“做”,这是发射块键盘上按下按钮,将焦点移到下一个文本派出细胞:

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 
+0

你有没有尝试过这样的代码说10个单元格,它是否仍然去下一个文本字段即使单元没有加载到内存中呢? – AdamM 2013-02-25 11:04:29

+0

是的,原因,它会通过调用线[self_.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:(indexPath.row + 1)inSection:0]]内部didEndOnExitHandler – iiFreeman 2013-02-25 12:26:10

+0

加载你能告诉我你是如何将块代码函数添加到您的表请给我一些细胞,这样我就可以看到你是如何实现它的,例如你可以告诉我你用来设置didEndOnExitAction的代码。 – AdamM 2013-02-25 13:52:54

0
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]; 
+0

您是否看过我的问题?这不回答我的问题,这只是一种不同的方式来设置下一个先前和完成的按钮,我已经知道如何设置 – AdamM 2013-02-25 10:10:08

相关问题