Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UITableViewCell's index from its edited UITextField

I'm working on an iPhone based BI project.

I have a UITextField in a UITextViewCell, the UITextFieldDelegate points to my UITableViewController, I haven't done any sub-classing for the UITextViewCell nor the UITextField.

Now after the text field end editing on the delegate

  -(void)textFieldDidEndEditing:(UITextField*)textField

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

Thank you. May the Force be with you.

like image 207
Zhao Xiang Avatar asked Apr 20 '11 15:04

Zhao Xiang


Video Answer


4 Answers

With the changes in the UITableViewCell class in iOS 7 you have to have a more dynamic way to get to the parent cell. You can get to the UITextField's cell by using the following snippet (which you should put in 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's index path
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)textFieldSuper];

The snippet works in both iOS 6 and iOS 7.

like image 75
nikolovski Avatar answered Oct 21 '22 16:10

nikolovski


The real question you are asking is about how to know where to save changes to your data model when the user enters data into a text field within a table cell.

@Ole Begemann's suggestion to use the tag property is perfectly valid. It depends on your data model. If all you need is a single integer to identify where the value gets in your model then 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 index path.

Keep in mind that all views have a tag property. So you could store the row index in cell.contentView.tag and the column index in textField.tag. From the text field you get the content view with textField.superview. If a different view is the superview of textField use that instead.

If you need something more complex than that to identify the location in your model to save the text field contents then you'll need to do something different. I would either subclass UITableViewCell or UITextField and store whatever info you need in a property you define in the subclass.

like image 35
XJones Avatar answered Oct 21 '22 14:10

XJones


If you have added your UITextField in UITableViewCell as Subviews, So you could access the UITableViewCell from your UITextField's superview property.

Try with below.

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

Let me know for any issue you face .

Edited:

you will get the row from tag property of your UITextField So

NSUInteger  myRow = myTextField.tag;

As you said your application does'nt support have multiple sections,it means you have all your rows at 0 section.

NSUInteger  mySection = 0;

Now Construct a NSIndexPath instance from the above information.

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

UITableViewCell* myCell = [myTableView cellForRowAtIndexPath:myIndexPath];

Now you have myCell which is at myRow in your UITableView

like image 4
Jhaliya - Praveen Sharma Avatar answered Oct 21 '22 15:10

Jhaliya - Praveen Sharma


For iOS 7, it's a bit different. If aTextField is an object of kind: UITextField iOS 6: [[aTextField superView] superView]

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

Apple has added a ScrollView between UItableViewCell and its contentView.

Edit: The scroll view is gone in iOS 8.

iOS 8: [[aTextField superView] superView]

like image 3
lenhhoxung Avatar answered Oct 21 '22 15:10

lenhhoxung