I have a custom uitableviewcell with a textfield in it. To input text into the textfield I am using a custom keyboard. However, when a keyboard button is pressed, my method fails to update the text in the textfield. When I check the cell that I've grabbed, I find that it is empty. Here is the code I use:
VariableCellController *cell = (VariableCellController *)[equationViewController.variableTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[equationViewController.selectedVariableRow intValue] inSection:0]];
Where VariableCellController is the uitableviewcell controller and the equationViewController is a property of my CustomNumberPadViewController.
It seems as if my instantiation of equationViewController is not the one actually showing...
Got it, I added a textfield as a property in my keyboard controller class and passed the handle to the text field to the keyboard controller in the textfielddidbeginediting method.
Related
There is CustomCell in tableviewcontroller .And in which there is dynamic txtfield. which is viewed by tag value. I am saving textfield value on dictionary on "textFieldShouldEndEditing". I want to save textfield value on button (Textfied is dynamic,no outlet is created for textfield).
First Image attached
I want to edit the saved address. Second Image attached
kindly revert asap
If i understood your question properly then,
You need to set the button (Which i guess is also inside the custom cell), same tag as that of the indexpath of the cell.
By that you can get the indexPath of the cell and get the cell by
MyCustomCell *cell = [self.tableview cellForRowAtIndexPath:button.tag]
Then, you can get the textfield inside the cell also by just calling cell.textFieldName.text, and you will get the text.
For more clarity the IBAction method of the button, should be something like this.
-(IBAction)buttonClickedToSaveTexd:(id)sender{
UIButton *btnOfCertainCell = (UIButton*)sender;
MyCustomCellClass *currentCell = [self.tableView cellForRowAtIndexPath:btnOfCertainCell.tag];
NSString *textOfTheCell = currentCell.textFieldName.text;
}
Again, for this to work, in cellForRowAtIndexPath you need to assign the button tag as indexPath.row. You dont need to set tag of the textfield.
When I use base cell (UITabelViewCell) with disclosure indicator and enabled selection, selection occurs by click anywhere in row.
But when I use custom class for cell with UITextField selection occurs only by click outside UITextField. How can I fix it?
you can set the userInteractionEnabled = NO for textfield.
but if you do this, the textfield won't be selected anymore.
When you use a custom cell which has UITextField embedded inside it, the click on UITextfield will call the UITextField Delegate method not any UITableView delegate method. If you need to select the Custom cell you have to write the code explicitly inside the UITextField delegate method. In order to identify which cell has the UITextField you can use tag property of UITextField which can mention the cell identifier.
I have a subclass of UITableViewCell that loads a nib on the parent ViewController viewDidLoad event - where the table view is located.
There's a UITextView inside this UITableViewCell.
The idea is that the user enters text in this textView, and when a button is pressed it will save this text (answers for a questionnaire) in NSUserDefaults.
My problem is that I cannot read the text from the textviews. When the user enters text and the table view is scrolled up and down, I can see the text is kept, so it must be stored in memory somewhere, this happens in cellForRowAtIndexPath.
For gathering the text I call this same method but the returned cell is nil. So it seems the text is kept when scrolling but if the method is called programmatically the cell is not returned.
I'm not sure how to keep track of this text, I have also tried the method didEndDisplayingCell but sometimes it doesn't get called so it's not reliable.
Nib registration:
UINib *nib = [UINib nibWithNibName:#"QuestionCell" bundle:nil];
[[self tableViewOutlet] registerNib:nib forCellReuseIdentifier:cellIdentifier];
cellForRowAtIndexPath:
QuestionCell *cell = [tableViewOutlet dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell.indexPath){
cell.indexPath = indexPath;//Setup cell ...
Trying to get the text inside the textView inside the cell (returns nil if not visible):
QuestionCell *cell = (QuestionCell *)[tableViewOutlet cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
You shouldn't rely on your tableview to retain textfields with the proper text entered. As soon a TableViewCell is scrolled off the screen, it'll get reused by the TableView and the value that the user has entered will likely be overwritten.
Instead, you should register your ViewController as the UITextView's delegate, and update your TableView's datasource object with every key press. Then, you'll be able to read the user's entered text off of that datasource object whenever you need.
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?
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.
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;
}
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.
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];
I have a weird bug. I have a grouped UITableView with static cells and I want to add a white shade under each section. The way I thought about doing it is to add a shadow to each lowest cell in a section.
So in viewDidAppear (because in viewDidLoad it wouldn't work) I wrote:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// change the cells shadow
The problem is that in viewDidLoad I call becomeFirstResponder on a text field. When cellForRowAtIndexPath is called, the textfield looses focus.
I tried using becomeFirstResponder in viewDidAppear after the call to cellForRowAtIndexPath but that doesn't help, the textfield still looses focus.
How can I fix this?
I recommend you to use the tableView:willDisplayCell:forRowAtIndexPath: method for adding the shadow.
Use the following code.
[textfield performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0];