Question

I've a grouped UITableView with several custom cells where I placed text fields. I have defined a property pointing to the text field that is currently active:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
   self.activeTextField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
   self.activeTextField = nil;
}

I need to get the row and section of the cell containing such text field. I tried this:

UITableViewCell *parentCell = (UITableViewCell *)[self.activeTextField superview];
NSIndexPath* cellPath = [self.tableView indexPathForCell:parentCell];
NSInteger cellRow = [cellPath row];

But I always get a 0. It looks like parentCell is not an UITableViewCell actually (but it should be). How can I get those values?

Thanks

Était-ce utile?

La solution

If you are adding the text view to cell's container view then you have to call self.activeTextField.superview.superview. If not, self.activeTextField.superview should work.

In order to find out if your text field is added to container view's cell:

  1. If you add the text field from xib/storyboard to a custom cell then it is in contentView
  2. If you do [cell addSubview:] then is in cell's view
  3. If you do [cell.cotentView addSubView:] then is in container view.

Autres conseils

Totally unsure if it works but you might try:

UITableViewCell *cell = [self.tableView cellForRowAtPoint:[self.activeTextField center]];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top