I\'ve created a table where each cell holds both a text label and a text field. I\'m adding the textfields as such [cell addSubview:passwordField];
and from a v
Why not have a datasource mapped to the TableView
and just retrieve / update the values in the datasource. You can then call reloadRowsAtIndexPaths
to load just the row you just changed. Trying to iterate through the TableView
rather than just updating the datasource seems very inefficient.
Instead of UIView* subView = [[cell.contentView subviews]lastObject];
you can try to find it as:
for(UIView *view in [cell subviews])
{
if([view isKindOfClass:[UITextfield class]]){
// view is the reference to your textfield
}
}
That way you can add other UIView
s as subviews and still get the reference of the textfield without having to keep track of its subview index.
You have added your textField
on subView of cell.
[cell addSubview:passwordField];
While you're trying to find it on cell.contentView
.
Add your textField
as a subView of cell.Contentview
[cell.contentView addSubview:passwordField];
And find it in this way -
for(UIView *view in [cell.contentView subviews])
{
if([view isKindOfClass:[UITextfield class]])
{
UITextField *textField = (UITextField *)view;
NSLog(@"%@",textField.text);
}
}
2 things occur to me:
In the long run it'll be easier to create a UITableViewCell
which contains a UITextField
which is accessible as a property. You can either use a nib to layout the cell or do it programmatically in the cells init method. This approach will make your code easier to manage.
You need to consider cell reuse. If you are reusing cells (which you should be) then you will need store the fetch the value from the textfield before it is reused.