Reputation: 11
In my app i have a login form, where user enter one field password. so i added a subveiw of UITextField
in UITableViewCell
during function call of cellForRowAtIndexPath
.
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"loginViewCell"];
UITextField *editableTextField = nil;
// Done Some settings of editableTextField
// Adding Subview
[cell addSubview:editableTextField];
When user press login button i called a selector function name login
UITableViewCell *cell = [loginTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
// Then i travesed in the subviews
of cell
and from UITextField
subview i extract password which was entered by user.
for (UIView *view in cell.subviews) {
if ([view isKindOfClass:[UITextField class]]) {
usernameField = (UITextField *)view;
break;
}
}
This is working fine till IOS 7
After searching on net i trace the problem that in IOS 7
Apple changes the view hierarchy of UITableViewCell
and now there is additional class inserted UITableViewCellScrollView
.
I debug my code in my selector function login it is getting the same cell in which i added the subview
i print the name of cell.subview
is is showing UITableViewCellScrollView
previously it was showing UITableViewCellcontentView
(before ios7
)
How can I extract the password from subeview
of UITableViewCell
?
Upvotes: 0
Views: 949
Reputation: 6115
You could do 2 things: If you have only have 1 textfield for you entire viewcontroller, you define a property that holds a reference to your passwordTextField
@property(strong, nonatomic) UITextField *passwordTextfield;
Or, if you have a textfield for each tableviewCell, you could define a collection of UITextFields
@property(strong, nonatomic) NSMutableArray *textFields;
then you can reference your passwordTextField with:
UITextField *passwordField = self.textFields[PASSWORD_ROW];
Upvotes: 0
Reputation: 6211
The easiest way would be to subclass your UITableView cell and add a class property that is your UITextField. Then you could just call cell.textField.text
instead of searching through the view hierarchy.
The second way would be to search recursively through the entire view hierarchy, not just a single layer of subviews. (and you should be searching the cell.contentView anyway, bad things happen when you add views as subview's of the cell directly.)
EDIT adding code for searching recursively through view hierarchy. I do not recommend this method, I recommend subclassing UITableViewCell (it will make your like so much easier), but here you go.
You would call a function like
UIView *yourFoundSubview = [self findTextFieldInCell:cell];
And that function would be defined:
-(UIView*)findTextFieldInCell:(UIView*)input
{
if([input isKindOfClass:[UITextField class]])
{
return input;
}
UIView *foundview;
for(UIView *view in input.subviews)
{
foundview = [self findTextFieldInCell:view];
if(foundview)
{
return foundview;
}
}
return nil;
}
Upvotes: 1
Reputation: 10127
The cellForRowAtIndexPath:
method is not the right place to add subviews but rather to manipulate them.
You should really create a subclass of UITableViewCell
having a @property UITextField
which is added to the cell within the initWithStyle:reuseIdentifier:
method.
[self.contentView addSubview:self.passwordField];
Then you can access this particular UITextField
with [cell passwordField]
or whatever you want to call it.
Upvotes: 1
Reputation: 10520
I believe you need to get the subviews of the UITableViewCellScrollView
.
Instead of adding the textfield to the cell, add the UITextField
to cell.contentView
, and look for the textField
in the subviews of the cell.contextView
.
I think what would really be best in the long run for your solution though, would be to create a custom UITableViewCell
, and add the textField
in there. You could directly access your textfield that way without having to loop through the subviews of the cell.contentView
.
Upvotes: 0