Ios 获取UITableViewCell中UITextField的行

Ios 获取UITableViewCell中UITextField的行,ios,uitableview,uitextfield,Ios,Uitableview,Uitextfield,在UITableView的自定义单元格中有两个UITextFields。 我需要编辑和存储文本字段的值。 当我在UITextField内单击时,我必须知道它所属的行,以便将值保存到本地数组的正确记录中。 如何获取textField的行索引? 我试过: -(void)textFieldDidBeginEditing:(UITextField *)textField { currentRow = [self.tableView indexPathForSelectedRow].row;

在UITableView的自定义单元格中有两个UITextFields。 我需要编辑和存储文本字段的值。 当我在UITextField内单击时,我必须知道它所属的行,以便将值保存到本地数组的正确记录中。 如何获取textField的行索引? 我试过:

-(void)textFieldDidBeginEditing:(UITextField *)textField
{

     currentRow = [self.tableView indexPathForSelectedRow].row;


}
但当我在UITextFieldRow内单击时,currentRow不会更改。它仅在我单击“选择整行”时更改…

尝试此操作

//For ios 7

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


//For ios 6

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

1> 可以通过编程方式在CellForRowAtIndexPath中创建文本字段,并将文本字段的标记设置为indexpath.row来实现。 然后textfielddibeginediting您可以只获取textField.tag并实现您想要的


2> 另一种方法是在一个表视图中有两个自定义单元格。这样,您可以单独放置文本字段,并从实用程序面板设置它们的标记。

文本字段没有向表视图发送触摸事件,因此indexPathForSelectedRow不起作用。您可以使用:

CGPoint textFieldOrigin = [self.tableView convertPoint:textField.bounds.origin fromView:textField];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:textFieldOrigin]; 

在iOS 8中,我发现模拟器和设备具有不同数量的超级视图,因此这是一种更通用的方法,应该适用于所有版本的iOS:

UIView *superview = textField.superview;
while (![superview isMemberOfClass:[UITableViewCell class]]) { // If you have a custom class change it here
    superview = superview.superview;
}

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

我要做的是创建一个自定义单元格,将我需要的任何自定义UI元素放入其中,并创建一个属性indexath,该属性在单元格出列时设置。然后我将indexPath传递给didSet中的自定义元素

在TableView中:

然后我实现UITextFieldDelegate协议,因为textField有它的indexPath,所以您总是知道它来自哪里。 不确定在何处设置代理的最佳位置。最简单的方法是在单元退出队列时设置它

override func textFieldDidEndEditing(_ textField: UITextField) {
    guard let myTextField = textField as? TableViewTextField else { fatalError() }
    guard let indexPath = myTextField.indexPath else { fatalError() }
 }

为什么是3次superview?tableviewCells的层次结构发生了变化,因此添加了一个额外的superview。您能给我指一下解释此变化的文档吗?考虑到这种方法的有效性,当苹果做出改变时,它肯定会失败,并且只会导致问题……不,因为我有2个。我知道你有2个文本字段。而不是直接将文本字段放入单元格。你有多少文本字段与决定你在哪一行无关,让所有文本字段与行具有相同的标记他需要单独标识文本字段,这就是为什么我建议他在CellForRowAtIndexPath中创建后标记textfield,并使用标记在TextFieldDiBeginediting中标识TextFields。我想这是一个健壮的解决方案。被接受的答案依赖于超级视图层次结构,并且是特定于操作系统的,所以现在它对我来说是一个糟糕的解决方案。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "EditableCell") as! EditableTableViewCell
        cell.indexPath = indexPath
        return cell
}
override func textFieldDidEndEditing(_ textField: UITextField) {
    guard let myTextField = textField as? TableViewTextField else { fatalError() }
    guard let indexPath = myTextField.indexPath else { fatalError() }
 }