Get UITextField string inside UITableViewCell

I have two UITextFieldsinside a custom cell UITableView. I need to edit and save the values โ€‹โ€‹of textFields. When I click inside UITextField, I need to know the string to which it belongs in order to save the value in the correct entry of the local array. How can I get the index of a textField string? I tried:

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

     currentRow = [self.tableView indexPathForSelectedRow].row;


}

But currentRow does not change when I click inside UITextFieldRow.It changes only when I click (select) the whole line ...

+4
source share
5 answers

try it

//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];
+6
source

, indexPathForSelectedRow . :

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

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

1 > , CellForRowAtIndexPath indexpath.row. textFieldDidBeginEditing textField.tag , .

2 > , 2 . , feild .

0

, , , , indexPath, , . indexPath didSet.

class EditableTableViewCell: UITableViewCell {

    @IBOutlet weak var textField: TableViewTextField!

    var indexPath: IndexPath? {
       didSet {
           //pass it along to the custom textField
           textField.indexPath = indexPath
        }
    }
}


class TableViewTextField: UITextField {
     var indexPath: IndexPath?
}

TableView:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "EditableCell") as! EditableTableViewCell
        cell.indexPath = indexPath
        return cell
}

UITextFieldDelegate, textField indexPath, , . , . - , .

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

All Articles