How to get UITableViewCell index from its edited UITextField

I am working on an iPhone based iPhone.

I have a UITextField in a UITextViewCell , a UITextFieldDelegate points to my UITableViewController , I have not done any subclass for UITextViewCell and UITextField .

Now after editing the text field on the delegate

  -(void)textFieldDidEndEditing:(UITextField*)textField 

I need to know the row index of the current cell I'm editing, is there a way to get the parent cell of the text field? Or can I set some property in a text box, for example, "rowIndex" when I create a cell? I really need this inorderto value to save new text.

Thanks. May the Force be with you.

+6
source share
8 answers

The real question you ask is how to find out where to save changes to your data model when a user enters data in a text box inside a table cell.

@Ole Begemann suggests using the tag property. It depends on your data model. If you need just one integer to determine where the value gets in your model, store that integer in the tag property of each text field. Since you are not using sections, the row index is equivalent to having the entire pointer path.

Keep in mind that all views have a tag property. That way you can save the row index in cell.contentView.tag and the column index in textField.tag . From the text box, you get a view of the content using textField.superview . If the other view is a textField supervisor, use this instead.

If you need something more complex to identify the location in your model in order to preserve the contents of the text field, you need to do something else. I would either subclass UITableViewCell or UITextField and UITextField any information you need in the property that you define in the subclass.

+8
source

With changes to the UITableViewCell class in iOS 7, you should have a more dynamic way to access the parent cell. You can go to the UITextField cell using the following snippet (which you must enter in the textFieldDidEndEditing: delegate method)

 // Get the cell in which the textfield is embedded id textFieldSuper = textField; while (![textFieldSuper isKindOfClass:[UITableViewCell class]]) { textFieldSuper = [textFieldSuper superview]; } // Get that cell index path NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)textFieldSuper]; 

The snippet works in both iOS 6 and iOS 7.

+9
source

If you added a UITextField to the UITableViewCell as Subviews, this way you can access the UITableViewCell from your UITextField's superview .

Try below.

 UITableViewCell* myCell = (UITableViewCell*)textField.superview ; 

Let me know about any issues you are facing.

Edited by:

you will get a string from the tag property of your UITextField So

 NSUInteger myRow = myTextField.tag; 

As you said, your application does not support multiple sections, this means that you have all your lines in section 0.

 NSUInteger mySection = 0; 

Now create an instance of NSIndexPath from the above information.

 NSIndexPath *myIndexPath =[NSIndexPath indexPathForRow:myRow inSection:mySection]; //Do'nt release myIndexPath instance. UITableViewCell* myCell = [myTableView cellForRowAtIndexPath:myIndexPath]; 

You now have myCell , which is on myRow in a UITableView

+4
source

For iOS 7, this is slightly different. If aTextField is an object of the form: UITextField iOS 6: [[aTextField superView] superView]

iOS 7: [[[aTextField superView] superView] superView]

Apple added a ScrollView between the UItableViewCell and its contents.

Edit: The scroll view is missing in iOS 8.

iOS 8: [[aTextField superView] superView]

+3
source

Like others, you can get the current cell by contacting the text box supervision. From there, you can easily find the index path of the current cell using the UITableView method - indexPathForCell:.

Update

Although I still think this is the easiest and best way to do this, it is possible that this can cause a slowdown in a large table view (if -indexPathForCell: includes a linear search). I just read another interesting idea: I present the section and the string as unsigned shorts, pack them into a single whole and save the result in the property of the presentation tag (text field) ( source ).

+2
source

You can set textfield.tag to the row index when you return the cell to tableView:cellForRowAtIndexPath:

+1
source

Keep in mind that if you have a long table of cells, they may be freed due to the size of the UITableView frame.

Since iOS reuses a cell if you have several similar cells, try to do this: start editing the cut cell on a UITextField, scroll down to another cell until the selected UITextField is displayed, and now see what happens in the -(void)textFieldDidEndEditing:(UITextField*)textField method -(void)textFieldDidEndEditing:(UITextField*)textField . You will notice that textFiled.superview is a NULL pointer since the cell has been freed and you can no longer access it.

The only solution that comes to my mind is to save the cell attributes that you need in the property using the method -(void)textFieldDidBeginEditing:(UITextField*)textField .

0
source

In fact, there is no need to worry about which cell index is currently being edited. I am using this code

 UIView *tableViewCellView = [[field superview] superview]; if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { // Need to traverse one step more up the visual tree tableViewCellView = [tableViewCellView superview]; } [self.tableView scrollRectToVisible:[tableViewCellView frame] animated:YES]; 

in combination with the BSKeyboardControls delegation method to successfully scroll through the selected field in the view. Tested with ios5 and up

0
source

Source: https://habr.com/ru/post/886323/


All Articles