2013-02-13 16 views
0

我有一个ViewController与分组的tableview添加。一些单元格包含文本字段。当用户按下返回键时,我想将第一个响应者切换到单元格列表中的下一个文本字段。然而,我无法让它工作,我不知道我是否选择了错误的单元格或选择了不正确的文本字段。移动到下一个UITextField在不同的单元格分组TableView

我用下面的设置自己的广告代码中的cellForRowAtIndexPath ..

cell.tag = ((indexPath.section + 1) * 10) + indexPath.row; 

这将创建与十位是一个区间值与个位作为行值的标签。即。标签11是一款零行1

这里是我的textFieldShould返回

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

[textField resignFirstResponder]; 

UITableViewCell *cell = (UITableViewCell *)textField.superview; 
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 

cell = [self tableView:_tableView cellForRowAtIndexPath:indexPath]; 

if (indexPath.section == 0) 
{ 
    if (indexPath.row == 0) 
    { 
     [[cell viewWithTag:((indexPath.section + 1) * 10) + (indexPath.row + 1)] becomeFirstResponder]; 
    } 
    if (indexPath.row == 1) 
    { 
     [[cell viewWithTag:((indexPath.section + 2) * 10)] becomeFirstResponder]; 
    } 
} 
return YES; 
} 

最后一个音符,目前新标签的增量硬编码的代码,但我希望能去到新标签,而不需要每次硬编码实际值。这可能吗?

回答

3

如果你的所有单元格都包含1个UITextField,我会说你可以继承UITableViewCell并添加一个引用单元格文本字段like I did here的属性。

但你说只有一些单元格包含一个文本字段,所以另一种选择是创建一个指向UITextFields的指针数组(我得到了这个想法here)。然后按Return键的用户会像这样循环遍历它们:

- (BOOL) textFieldShouldReturn:(UITextField*)textField { 
    NSUInteger currentIndex = [arrayOfTextFields indexOfObject:textField] ; 
    if (currentIndex < arrayOfTextFields.count) { 
     UITextField* nextTextField = (UITextField*)arrayOfTextFields[currentIndex+1] ; 
     [nextTextField becomeFirstResponder] ; 
    } 
    else { 
     [textField resignFirstResponder] ; 
    } 
} 
+0

我明白了,我会试试看看它是如何工作的。 – JMD 2013-02-13 17:34:38

+0

好的,这种方法可以正确切换焦点,但是我想让焦点切换时键盘消失。目前它保持在屏幕上,直到到达最后一个文本框。强制数据进入所有不需要的文本字段。 – JMD 2013-02-13 18:06:20

+0

如果您希望键盘在用户点击返回时消失,那么您的'textFieldShouldReturn'方法需要做的唯一事情就是'[textField resignFirstResponder];'。这是否回答你的问题? – 2013-02-13 19:45:13

1

将tag设置为您的textField而不是单元格。

yourTextField.tag = ((indexPath.section + 1) * 10) + indexPath.row; 
相关问题