Search code examples
iosobjective-cuitableviewuitextfielddelegate

Referencing UITextFields in a custom cell in a UITableView


I have a static UITableView that has 5 cells like below:

custom UITableViewCell

When the user selects the top row I need to execute a method which will populate data into the second row.

When de-queing the cell's I added a target action like so:

[cell.upperTextField addTarget:self action:@selector(upperTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

I get notified of the event. How do I reference the second text view (find the CellForRowAtIndexPath) to enter data into it?

I can't use the didSelectRow since I have disable selection - the user can only tap into one of the provided TextFields.


Solution

  • Use a custom cell for that and find your cell object using CellForRowAtIndexPath method and now you can easily find your textfield like below.

    MyCellView myCell = [myTableView cellForRowAtIndexPath:<your index path>];
    myCell.firstTextField 
    

    or

    myCell.secondTextField
    

    If you want to change the text. You can easily change the text of textField.