-1

I have a simple project that contains a UITableView with a custom cell, in this I cell have a UITextField, So I have a button that calls a function 'addField', When This function is called I increment +1 value for the variable numberOfRows, then I call the command responsible for updating the table, as you can see bellow:

-(void)AddField{

    numberOfRows++;
    [tableView reloadData];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return numberOfRows;
}

Doing so creates a new table row with a new textField to enter another value.

Inside My cell I connect the UITextField with the Outlet,:

 IBOutlet UITextField *myFields;

Inside awakeFromNib I have a code who checks whether the user is typing:

- (void)awakeFromNib
{
    [campoTexto addTarget:self
             action:@selector(change:)
   forControlEvents:UIControlEventEditingChanged];
}

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

    NSLog(@"What The textField I'm Typing? -> %ld",(long)textField.tag);

}

All I would do is try to find some way to differentiate which text field I'm typing, as well UITableView can differentiate their rows (0 .. n), I think I can differentiate this UITextField (0 ... n).

Can anyone give me some help, or maybe know a tutorial on the web that explain this kind of thing?

Thanks.

  • After a quick google search, you'll find that this question has been answered several times. Here is a good answer from @rmaddy: http://stackoverflow.com/questions/17757806/how-to-get-a-uitableviewcell-from-one-of-its-subviews – Jonathan May 19 '14 at 01:05

1 Answers1

1

The best way to Identify custom controls in a tableView cell, or rather what I would prefer is to set a tag for the custom control in your tableView datasource method cellForRowAtIndexPath:.

cell.yourTextField.tag = indexPath.row;

You can then get the indexPath from the corresponding tag in your method as follows:

-(void)change:(UITextField *)textField
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag inSection:0];
}

Hope this helps :) !

GoGreen
  • 2,251
  • 1
  • 17
  • 29