How to get UITableViewCell index from UITextField?

I have a UITextField in a user cell inside a table. I created a new DataCell class, which is a subclass of UITableViewCell. Inside the DataCell, I created points for the text fields, and I also have a method inside the implementation file that uses β€œedit completed”, and I control the values ​​of the textField.

Now I am wondering how to get rowIndex or cell number, since every time I click the + button, a new user cell is loaded into the table. If I get a tag, I always get the same tag number, regardless of the cell I selected.

+4
source share
4 answers

The text field passed to your delegate is a sub-element of the contentView cell.

 UITableViewCell *cell = (UITableViewCell*) textField.superview.superview; NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
+10
source

You can use this logic if you are not sure about the hierarchy between the text box and the cell.

 UITableViewCell *cell = nil; UIView *parentView = textField.superview; while(parentView) { if([parentView isKindOfClass:[UITableViewCell class]]) { cell = parentView; break; } parentView = parentView.superview; } if(cell) NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
+2
source

Add tags to the text box in the tableView: cellForRowAtIndexPath: method. In this example, I have a custom cell with a label and a text field:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; cell.label1.text = self.theData[indexPath.row]; cell.textField.tag = indexPath.row; return cell; } 

It looks like you might be working with the end of editing in your custom cell class, but you might want to do this in the table view controller, as this gives you easy access to the model, and I suppose you are changing what the user types into text field. If you do this, you must connect the text field delegation property to the table view controller in IB.

0
source

If we accept fragile answers, then in order to bring something new into the conversation:

 CGRect rectInTableView = [tableView convertRect:textField.bounds fromView:textField]; NSUInteger indexOfCellContainingTextField = (NSUInteger)(rectInTableView.y / tableView.rowHeight); 

Assumptions that make it fragile: (i) all rows are the same height; (ii) the height is set in table form. If you have not executed UITableViewDelegate -tableView:heightForRowAtIndexPath: then both of these assumptions will be considered true. You also take advantage of the fact that casting a positive float into an integer is rounded down.

I would say that although it is still not completely excluded from the assumptions, it is less fragile than the Mundi decision, because it only makes assumptions about the things that you directly control (i.e., about calibrating the cell), and not about what you are not doing (i.e. the view hierarchy UIKit uses internally to represent table views).

0
source

All Articles