2013-12-12 55 views
0

我在UItableView中有一些UITextfields。文本字段已经在我创建的UITableViewCell中进行了分类。在按下键盘上的返回键后,我会知道如何进入下一个单元格。跳转到下一个UItextField

因此,如果用户是开始宽,他们将继续向下宽度科拉姆,或者如果他们想进入高度每次他们打的时间进入,他们将继续沿着列。

目前发生了什么是如果我使用宽度科拉姆,它工作正常,但如果我按下回车键后选择一个高度的UITextField它跳转到下一个单元格的宽度。

这是我检测返回键的代码。

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [[self.view viewWithTag:textField.tag+1] becomeFirstResponder]; 
    return YES; 
} 

这就是我的单元格tableview的样子。

enter image description here

任何帮助,将不胜感激。

欲了解更多信息,这是我如何将自定义celltextfields添加到UItextfield。

NSString *widthString = [currentFinishingDictionary objectForKey:@"w"]; 
    if ((NSNull *) widthString != [NSNull null]) { 
     cell.widthTexField.text = widthString; 
     cell.widthTexField.delegate = self; 
     cell.widthTexField.tag = indexPath.row; 
    } else { 
     cell.widthTexField.text = @" "; 
     cell.widthTexField.delegate = self; 
     cell.widthTexField.tag = indexPath.row; 
    } 

    NSString *heightString = [currentFinishingDictionary objectForKey:@"h"]; 
    if ((NSNull *) heightString != [NSNull null]) { 
     cell.heightTextField.text = heightString; 
     cell.heightTextField.delegate = self; 
     cell.heightTextField.tag = indexPath.row; 
    } else { 
     cell.heightTextField.text = @" "; 
     cell.heightTextField.delegate = self; 
     cell.heightTextField.tag = indexPath.row; 
    } 
+0

单个单元格是否包含1个宽度文本字段和1个高度文本字段? – sanjaymathad

+0

是的,它的剂量.... – HurkNburkS

回答

0

请确保您已设置标签UITextField

你应该尝试这种方式:

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    //[[self.view viewWithTag:textField.tag+1] becomeFirstResponder]; 
    // Get here you cell seleted Refrecne 
     id obj = [cell.contentView viewWithTag:textField.tag+1]; 

     if([obj isKindOfClass:[UITextField class]]) { 
     UITextField *nextField = (UITextField*)obj 
     [nextField becomeFirstResponder]; 
    } 
    return YES; 
} 

或者你可以使用ELCTextFieldCell

其内容的UITextField和的UILabel在一个细胞。您可以根据需要定制。

+0

obj在这种情况下返回零。 coudl是因为我subclassed UItableViewCell?我也会使用ELCTextFieldcell,但是由于其他视图以及表格本身的复杂性,我致力于UITavleview ..除了这两个文本字段外,还有更多内容。当你的子类化单元格时,明智的UITabeViews也很棒。 – HurkNburkS

+0

如果'obj'为零。那么你应该检查你是否给出了确切的标签值?确保用户点击UITextField时能够获得正确的单元格实例。如果UITableView中有很多东西,那么最好使用不同的单元标识符。 –

0

可以使用

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


UITableViewCell *cell = [self.myTableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow: textField.tag+1 inSection:0]]; 
UITextField *tf = (UITextField*)[cell viewByTag:textField.tag+1] 
[tf becomeFirstResponder]; 
     return YES; 
    } 
0

我认为这个问题可能是在标签被分配到UITextFields的方式。

请检查标签值是否实际上是您的假设。

+0

我给他们分配了两个indexpath.row编号,用于标记问题。我不知道该怎么办。 – HurkNburkS

+0

其实..如果我加0.5到安装标签它应该工作我htink ..不。 – HurkNburkS

+1

我想这意味着有两个TextFields具有相同的标签。如果是这种情况,您可以考虑给宽度文本字段编号100,101.102 ..等等和高度字段1000,1001,1002,1003 ..等即添加额外100或1000,同时设置标签。它可能解决你的问题。 – iOsDude

0
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     [yourtextfield resignFirstResponder]; 
     // save state, etc if necessary 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    MyCell * cell = (MyCell *)[tableView cellForRowAtIndexPath:indexPath]; 
    self.selectedField = cell.tf; // cell.tf is a UITextField property defined on MyCell 
    [yourtextfield becomeFirstResponder]; 
} 
0

你有没有尝试使用替代的tableview的CollectionView,收集意见的设计可以制成类似于您添加的图片,你可以在一个单元格添加一个文本框,然后做出宽度文本字段第一个响应者做这个

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
if ([tableView.dataSource tableView:tableView numberOfRowsInSection:0] > tag + 2) 
     CustomCell *cell = [self.myTableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow: textField.tag+2 inSection:0]]; 

     [cell.textField becomeFirstResponder]; 

} 

,如果你想仍然采用这种设计仍然存在,那么你可以做到这一点

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

    int xPosition = (int)textField.frame.origin.x; 

    CustomCell *cell = [self.myTableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow: textField.tag+1 inSection:0]]; 

    if((int)cell.textField1.x == xPosition) 
    { 
      [cell.textField1.x becomeFirstResponder]; 

    } 
    else 
    { 
      [cell.textField2.x becomeFirstResponder]; 
    } 

}

代码未编译。只有逻辑写。希望它有帮助:)

0

更改您的分配标记值,如下所示,可能会对您有所帮助。

NSString *widthString = [currentFinishingDictionary objectForKey:@"w"]; 
    if ((NSNull *) widthString != [NSNull null]) { 
     cell.widthTexField.text = widthString; 
     cell.widthTexField.delegate = self; 
     cell.widthTexField.tag = indexPath.row; 
    } else { 
     cell.widthTexField.text = @" "; 
     cell.widthTexField.delegate = self; 
     cell.widthTexField.tag = indexPath.row; 
    } 

    NSString *heightString = [currentFinishingDictionary objectForKey:@"h"]; 
    if ((NSNull *) heightString != [NSNull null]) { 
     cell.heightTextField.text = heightString; 
     cell.heightTextField.delegate = self; 
     cell.heightTextField.tag = indexPath.row + 2000; 
    } else { 
     cell.heightTextField.text = @" "; 
     cell.heightTextField.delegate = self; 
     cell.heightTextField.tag = indexPath.row + 2000; 
    } 

现在您的textfields标记如下所示,并可以按照您的要求工作。现在

0 2000 
1 2001 
2 2002 
3 2003 
4 2004 
.. ........ 

你的方法能正常工作,但需要考虑其他的想法,以及因为这会错了,当你有2000个细胞。

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    // i.e. textField.tag == 6 then next 6 + 1 == 7 
    // i.e. textField.tag == 20006 then next 2006 + 1 == 2007 
    [[self.view viewWithTag:textField.tag+1] becomeFirstResponder]; 
    return YES; 
}