简体   繁体   中英

UITableView with Custom UITableViewCell

I have TableView I have a UITableViewCell. In UItableViewController I have UITextField. In application when I tap in TextField. It is open. But methods – tableView:willSelectRowAtIndexPath: – tableView:didSelectRowAtIndexPath: do not work, becouse we do not tap directly on row. How I can know the indexPath.row of current row?

In the cellForRowAtIndexPath method, assign the current row to your UITextField by setting it's tag property to the value of indexPath.row

[yourTextfield setTag:indexPath.row];

When the textfield finished editing, you can read out it's tag property and you're done.

You can either:

  1. Subclass UITextField and give it an indexPath property. In cellForRowAtIndexPath, when the textField is created and added to the cell, assign the property. Then in the textField delegate methods, you will have access to the correct indexPath. This method also allows you to associate other information with your textField that you may need.

  2. Use the UIControl's action methods:

     - (void)textFieldDidChangeValue:(id)sender withControlEvents:(UIEvent *)event { NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: [[[event touchesForView:sender] anyObject] locationInView:self.tableView]]; //Do whatever you need to, now that you have the indexPath } 

    This method avoids needing to subclass.

Something like this should work, as long as you have a reference to your tableView and the textField. The cell needs to be visible too:

UITableViewCell* tvc = (UITableViewCell*)[textField superview];
while ( tvc != nil && ![tvc isKindOfClass: [UITableViewCell class]] )
    tvc = [tvc superview];

NSIndexPath* ip = [tableView indexPathForCell: tvc];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM