Set focus to UITextField in tableCell - ios

I am creating a table view with UITextFields dynamically.
l_textField = [[UITextField alloc] initWithFrame:TextFieldFrame];
l_textField.tag = cellRow;
l_textField.delegate = self;
l_textField.font = [UIFont boldSystemFontOfSize:14];
l_textField.textColor = [UIColor blackColor];
[l_textField setEnabled:YES];
[cell.contentView addSubview:l_textField];
And now I want to set focus on this text field when user touch cell. I write this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {
UITextField* field = (UITextField *) [tblView viewWithTag: newIndexPath.row];
[field resignFirstResponder];
}
But this doesn't work

change [field resignFirstResponder]; with [field becomeFirstResponder];
resignFirstResponder is used to remove keyboard (focus) from text field

I've faced the same issue here, even using correctly the [field becomeFirstResponder];.
I also used tags to get the objects I have inside the cell. Your didSelectRowAtIndexPath should look like this to get the right cell:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
currentRow = indexPath.row;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UITextField *indexField = (UITextField *)[cell viewWithTag:101];
[indexField becomeFirstResponder];
}
It worked like a charm for me.

Swift 3
myTextField.becomeFirstResponder()
My Code. Take index path of current cell. And get cell of next indexpath.row. And called
** cell1.txtFindings.becomeFirstResponder() **
if let textFieldIndexPath = specsListView.indexPath(for: cell){
let newIndexPath = IndexPath(row: textFieldIndexPath.row + 1, section: 0)
if let cell1: SpecsTableViewCell = specsListView.cellForRow(at: newIndexPath) as? SpecsTableViewCell{
cell1.txtFindings.becomeFirstResponder()
}
}

Related

iOS UITableView cells getting duplicated

I have a tableviewcontroller that has dynamic controls created in cells. If it's a dropdown type, I take the user to a different tableviewcontroller to select the value. Once selected, I pop back and reload the data, but when I do that it overwrites the cells on top of one another. I know this is because I'm reusing the cells, but I cannot seem to figure out how to prevent it.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
EWHInboundCustomAttribute *ca = [visibleCustomAttributes objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.tag=indexPath.row;
if (ca.CustomControlType == 1) {
cell.detailTextLabel.hidden=true;
cell.textLabel.hidden=true;
UITextField *caTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 185, 30)];
caTextField.adjustsFontSizeToFitWidth = YES;
caTextField.textColor = [UIColor blackColor];
caTextField.placeholder = ca.LabelCaption;
if (ca.ReadOnly) {
[caTextField setEnabled: NO];
} else {
[caTextField setEnabled: YES];
}
caTextField.text=nil;
caTextField.text=ca.Value;
caTextField.tag=indexPath.row;
caTextField.delegate=self;
[cell.contentView addSubview:caTextField];
} else if (ca.CustomControlType == 4) {
cell.detailTextLabel.text=ca.Value;
cell.textLabel.text=ca.LabelCaption;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else {
cell.detailTextLabel.hidden=true;
cell.textLabel.hidden=true;
UITextField *caTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 185, 30)];
caTextField.adjustsFontSizeToFitWidth = YES;
caTextField.textColor = [UIColor grayColor];
caTextField.placeholder = ca.LabelCaption;
[caTextField setEnabled: NO];
caTextField.text = ca.Value;
caTextField.tag=indexPath.row;
caTextField.delegate=self;
[cell.contentView addSubview:caTextField];
}
return cell;
}
Instead of creating the UITextfield each time I would suggest at least using [UIView viewWithTag:tag] to capture the same UITextField object.
I'd suggest you to create custom UITableViewCell subclass and put all subviews related logic there.
Next, in order to reset/clear cell before reuse - you should override prepeareForReuse function.
Swift:
override func prepareForReuse() {
super.prepareForReuse()
//set cell to initial state here
}
First,I suggest you to use custom cells.If not and your cells are not so many,maybe you can try unique cell identifier to avoid cell reuse:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// unique reuseID
NSString *cellReuseID = [NSString stringWithFormat:#"%ld_%ld", indexPath.section, indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseID];
// do something
}
return cell;
}
Hope it's helpful.

Textfield data hide when scroll tableview

I am developing IOS App. Multiple textfield add on tableviewcell.when enter data on texfield than scroll tableview textfield data hide. How to manage textfield on tableview.Please Help me and thanks in advance
code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableViews{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// Return the number of rows in the section.
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MyIdentifier = #"cell";
UITableViewCell *cell;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
UITextField *textFieldName = [[UITextField alloc]initWithFrame:CGRectMake(8.0f, 3.0f, 80.0f, 36.0f)];
[cell addSubview:textFieldName];
[textFieldName setDelegate:self];
textFieldName.tag = indexPath.row+1;
UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(96.0f, 0.0f, 1.0f, 50.0f)];
line.backgroundColor = [UIColor colorWithRed:214.0/255.0 green:214.0/255.0 blue:214.0/255.0 alpha:1.0];
[cell addSubview:line];
[cell setSeparatorInset:UIEdgeInsetsZero];
tableView.allowsSelection=NO;
return cell;
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([textField tag]-1) inSection:0];
UITableViewCell *Cell = [_dairyTable cellForRowAtIndexPath:indexPath];
}
How to deal with UITextField Value in UITableView :
First of all you have to create mutable Array with count equal to the number of UITextField.
In cellForRow do cell.textField.tag = indexPath.rowsothat you can identify each UItextField.
Save every UITextFieldvalue in the array in the same sequence with each character change. So, do code in textdidChange delegate method of UITextField by identifing the UITextfield by its tag as textfield.tag.
Whenever user enters or edit the text, you get that text in array.
Now in cellForRow add below line:
cell.textfiled.text = arrDataText[index.row]

Write in uitextfield without press on it

I have created a custom cell with a textField that initially is hidden.
When I choose the cell, the textField appears and I have to press on textField to write.
This I want is when I choose the cell to write immediately in textField. This is my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ( indexPath.row == 0 ){
commentTextField.text = [NSString stringWithFormat:commentDetailTextLabel.text];
commentTextField.textColor = [UIColor yellowColor];
commentTextField.hidden = NO;
commentDetailTextLabel.hidden = YES;
}
}
Add [commentTextField becomeFirstResponder]; in your if body.

ios MasterDetail app: UITextField in tableview not work

I have a master detail app in ios, with SDK 7.0, Xcode 5, using ARC.
master has many items, detail has a table view. When I click on an item, the contents of tableview will change. This works well until I put a UITextField in each cell, because I want to edit the contents in the table.
The problem is: when I click on a new item, the old contents don't disappear,so the contents of a cell is a superposition of the new UITextField's text and the old UITextField's text.
The first normal tableview like this:
After I click on an new item, the tableview will like this:
The snippet of codes of master is:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
LHBPoetry *poetry = poetryArray[indexPath.row];
self.detailViewController.poetryId = poetry.poetryId;
}
I have tried a lot of things in the above method, for example, I make all instances of the detail view controller to be nil; table view's data array removeAllObejects; table view reloadData; It can't fix the problem.
The snippet of detail is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"detailCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UITextField *textField = textField = [[UITextField alloc] initWithFrame:
CGRectMake(90, 12, 200, 25)];
textField.tag = indexPath.row;
textField.text =_sentenceArray[indexPath.row];
textField.clearsOnBeginEditing = NO;
textField.delegate = self;
textField.returnKeyType = UIReturnKeyDone;
[textField addTarget:self
action:#selector(textFieldDone:)
forControlEvents:UIControlEventEditingDidEnd];
[cell.contentView addSubview:textField];
textField.text = _sentenceArray[indexPath.row];
return cell;
}
I draw this tableview in Main.storyborad, It has a prototype cell with an identifier.
Any help will be appreciated.
k there is something i want to tell, wy because u are keep on adding the textfields for reused cells, there is not one textfield in the cell ..:) there are more then one text field's, because of that u are getting overlapped with one other, i think u are using default "master- detail" application, and modifying it..:)
oky for that u need to modify like below
in master controller
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(2, 3, 300, 30)];
[textField addTarget:self action:#selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEnd]; //hear u are adding once initially
textField.tag = 100;
[cell addSubview:textField];
}
NSString *object = _objects[indexPath.row];//_objects is mutable array holding the sentences or names
UITextField *textField = (UITextField *)[cell viewWithTag:100];//after that u are reusing the textfields
textField.text = object;
textField.tag = indexPath.row;
return cell;
}
now you are creating the cell thats wy u dont want the prototype cell remove it from story board
in the above u removed the custom cell becz u are creating the cell in the code it self
now in the method
- (void) textFieldDone:(UITextField *)inTextFIeld
{
int index = inTextFIeld.tag;
[_objects replaceObjectAtIndex:index withObject:[inTextFIeld text]];
[self.masterTableVIew reloadData];//made connection to ur tableview
}

Values over UITextField placed over UITableViewCell are getting lost

I have placed UITextField over each UITableView cell. While scrolling the UITableView cell the text over the UITextField is getting lost. What can I do to prevent this?
You need to get track of an NSMutableArray which stores the text field values. Make the initial values #"". When you insert the text field into the cell, get the value from the appropriate value in the array.
If the text field exits, do something like this to store your text field values in the array:
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
UITableViewCell *cell=(UITableViewCell *)textField.superview;
NSIndexPath *indexPath = [table indexPathForCell:cell];
NSMutableArray *rows=[sectionArray objectAtIndex:[indexPath section]];
NSString *string=[rows objectAtIndex:indexPath.row];
string=textField.text;
[[sectionArray objectAtIndex:[indexPath section]]replaceObjectAtIndex:[indexPath row] withObject:string];
[textField resignFirstResponder];
return YES; }
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *myCellId= #"myDateCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCellId];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: myCellId];
CGRect textFieldFrame;
CGRect labelFrame;
labelFrame = CGRectMake(cell.frame.origin.x+45, cell.frame.origin.y+3, labelWidth, cell.bounds.size.height-5);
textFieldFrame = CGRectMake(cell.frame.origin.x+labelWidth+15, cell.frame.origin.y+12 ,cell.
UILabel *label = [[UILabel alloc]initWithFrame:labelFrame];
UITextField *textField = [[UITextField alloc]initWithFrame:textFieldFrame];
textField.keyboardType=UIKeyboardTypeNumberPad;
textField.delegate = self;
textField.text = [[sectionArray objectAtIndex:[indexPath section]]objectAtIndex:[indexPath row]];
[cell addSubview:label];
[cell addSubview:textField];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
It's because as you scroll, the tableview dynamically creates and destroys cells to save on memory. If you still haven't figured out a way to solve this problem let me know and I will help you find a solution

Resources