如何激活UITableView中的下一个和前一个UITextField?

问题描述:

我在我的应用程序中使用this one添加UITextFields我UITableViewCells,我现在处于亏损状态我如何通过使用Next按钮在工具栏上的键盘,在这里我的代码让用户跳转到下一个项目如何激活UITableView中的下一个和前一个UITextField?

IBOutlet UITextField *contNo,*breed,*coat,*color,*birthdate; 
IBOutlet UIToolbar *toolBar; 
IBOutlet UIBarButtonItem *barButton; 
NSArray *fieldsArray; 

fieldsArray = [[NSArray alloc] initWithObjects:birthdate,contNo, breed,homeAddress, coat,color, nil]; 

-(IBAction)next 
{ 
for (int i=0; i<[fieldsArray count]; i++) 
{ if ([[fieldsArray objectAtIndex:i] isEditing] && i!=[fieldsArray count]-1) 
    { 
     [[fieldsArray objectAtIndex:i+1] becomeFirstResponder]; 
     if (i+1==[fieldsArray count]-1) 
     { 
      [barButton setTitle:@"Done"]; 
      [barButton setStyle:UIBarButtonItemStyleDone]; 
     }else 
     { 
      [barButton setTitle:@"Done"]; 
      [barButton setStyle:UIBarButtonItemStyleBordered]; 
     } 
     break; 
      } 
    } 
} 

-(IBAction) previous 
{ 
for (int i=0; i<[fieldsArray count]; i++) 
{ 
    if ([[fieldsArray objectAtIndex:i] isEditing] && i!=0) 
    { 
     [[fieldsArray objectAtIndex:i-1] becomeFirstResponder]; 
     [barButton setTitle:@"Done"]; 
     [barButton setStyle:UIBarButtonItemStyleBordered]; 
     break; 
    } 
    } 
} 

-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
[textField setInputAccessoryView:toolBar]; 
for (int i=0; i<[fieldsArray count]; i++) 
{ 
    if ([fieldsArray objectAtIndex:i]==textField) 
    { 
     if (i==[fieldsArray count]-1) 
     { 
      [barButton setStyle:UIBarButtonItemStyleDone]; 
     } 
    } 
    } 
} 

当我用文本框外的tabelview下一个和以前的工作properly.The下一个和以前不与tabelview.Can里面里面的UITextField工作的任何一个请帮助我试试理清

#pragma mark - UITextField Delegate 

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    HRCFormCell * currentCell = (HRCFormCell *) textField.superview.superview; 
    NSIndexPath * currentIndexPath = [self.tableview indexPathForCell:currentCell]; 

    if (currentIndexPath.row != [self.questionsArray count] - 1) { 

     NSIndexPath * nextIndexPath = [NSIndexPath indexPathForRow:currentIndexPath.row + 1 inSection:0]; 
     HRCFormCell * nextCell = (HRCFormCell *) [self.tableview cellForRowAtIndexPath:nextIndexPath]; 

     [self.tableview scrollToRowAtIndexPath:nextIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 

     [nextCell.textField becomeFirstResponder]; 
    } 

    return YES; 
} 

请尝试以下代码可能会对您有所帮助。

+0

什么是HRCFormCell? – fazil 2013-04-10 12:40:36

+0

它是自定义单元格文件名称 – 2013-04-10 13:33:35

+0

以前怎么样?它不适合我 – karthikeyan 2016-03-17 10:29:20

在tableview单元格的contentview中添加textfield。 将标签设置为cellforrowatindexpath中的textfield,以使标签随着添加而增加。 标签可以是“3 + indexpath.row”形式

在文本中开始编辑找到当前文本字段的标签,保存标签。在接下来的 中,增加标签,使用标签查找文本字段并使其成为第一响应者。

或尝试此链接How to activate next UITextField in a UITableView?有由法比亚诺Francesconi http://blog.encomiabile.it/2013/02/08/how-to-activate-next-uitextfield-in-uitableview-ios/

这是旧的文章提供了很好的答案,但具有较高的网页排名,所以我会用我的解决方案插入内容。

我也有类似的问题,结束了创建的UIToolbar一个子类来管理下一/上/做一个动态的tableView功能与部分:https://github.com/jday001/DataEntryToolbar

您可以设置工具栏的文本字段的inputAccessoryView并添加他们到它的字典。这使您可以通过动态内容向前和向后循环。如果您希望在textField导航发生时触发自己的功能,则有委托方法,但您不必处理管理任何标签或第一响应者状态。

有代码片段&在GitHub链接的示例应用程序来帮助实现细节。您将需要自己的数据模型来跟踪字段中的值。