Write in uitextfield without press on it - ios

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.

Related

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]

restrict a textfield in table view from entering

I'm building an app which contains user details view. i have used table view to keep 3 textfield. what i want to do is, when user clicks edit button, 1st textfield(phone Number TF) should not allow user to enter data & in remaining 2 textfield should allow user enter data. thanks in advance
Set the userInteractionEnabled property:
//UITextField *textfield;
textfield.userInteractionEnabled = NO; // TO stop UserInteraction and Yes TO enable User Interaction
do like
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"XXXXX"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:#"SimpleTableCell"];
}
cell.yourTextfield.userInteractionEnabled = YES;
if (indexPath.row == 0) // it disable your interaction in first cell
{
cell.yourTextfield.userInteractionEnabled = NO;
}
return cell;
}
Choice-2
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"XXXXX"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:#"SimpleTableCell"];
}
cell.yourTextfield.tag = indexPath.row; // set tag for each textfield
return cell;
}
on that delegate fire disable the interaction
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
BOOL editable;
if (textField.tag == 0) {
editable = NO;
} else if (textField.tag == 1 || textField.tag == 2) {
editable = YES;
}
return editable;
}
You don't need a table view to present three textfields. Basically, tableview's used for present big amount of data.
But you could simply set userInteractionEnabled to NO to prevent changing textfield.
Otherwise, if you still want to use tableview you still can disable user interaction in tableView:cellForRowAtIndexPath:

iOS not able to update the set text in UITextField

I have a UITextField in UITableViewCell the text property is not updating.
My tableview delegate:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
KAPInputTableViewCell *cell = [self.registerTableView
dequeueReusableCellWithIdentifier:KAPInputCellIdentifier];
if (!cell) {
cell = [[KAPInputTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:KAPInputCellIdentifier];
}
switch (indexPath.row) {
case 0:
cell.inputTextField.placeholder = KAPLocalizedString(#"email", #"E-mail address");
cell.inputTextField.text = #"Test";
cell.tag = 0;
cell.errorLabel.text = self.emailError;
self.emailCell = cell;
self.emailTextField = cell.inputTextField;
break;
}
cell.inputTextField.delegate = self;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Then when I call,
NSLog(#"%#", self.emailTextField.text);
Later in my ViewController on the press of a button the edited text still returns "Test" while I changed the input value.
Please help.
I think you should just implement the UITextField delegate method:
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(#"%#", textField.text);
}
Here you will see your textField updated.

UIButton gets created twice on cell reload

I have a UIButton inside a UITableViewCell. When the app is first launched, it works as expected, where I created it's frame.
When I scroll pass the cell which holds the button, it creates a second instance of the button slightly below the button.
Here's a video to illustrate my problem: http://pixori.al/DJ1k
Here's the code for the UITableViewCell and also how I populate the cells.
Not sure why it's behaving like this.
#pragma mark - UITableViewDataSource
// 3 sections, (1 = mistarOverview) (2 = hourlyForecast) (3 = dailyForecast)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return MAX(6,6) + 1; //TODO add getNumberOfClasses for people with 7 or 8 classes
} else if (section == 1) {
return MIN([[MAManager sharedManager].hourlyForecast count], 6) + 1;
} else {
return MIN([[MAManager sharedManager].dailyForecast count], 6) + 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Redefine layout variables in method from `viewDidLoad`
CGFloat inset = 20; // For padding
if (! cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
// Sets up attributes of each cell
cell.selectionStyle = UITableViewCellSelectionStyleNone; //TODO none
cell.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
cell.textLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.textColor = [UIColor whiteColor];
QBFlatButton* loginButton = nil;
if (indexPath.section == 0) {
if (indexPath.row == 0) {
[self configureHeaderCell:cell title:#"Grades"];
if ([cell.textLabel.text isEqual: #"Grades"] && (!loginButton) && (indexPath.row == 0) && (indexPath.section == 0)) {
UIView *cellView = cell.contentView;
CGRect loginButtonFrame = CGRectMake((cellView.frame.size.width - (80 + inset)), 18, 80, (cellView.frame.size.height));
loginButton = [[QBFlatButton alloc] initWithFrame:loginButtonFrame];
[loginButton addTarget:self action:#selector(loginButtonWasPressed)forControlEvents:UIControlEventTouchUpInside];
loginButton.faceColor = [UIColor grayColor];
loginButton.sideColor = [UIColor clearColor];
loginButton.radius = 6.0;
loginButton.margin = 4.0;
loginButton.depth = 3.0;
loginButton.alpha = 0.3;
loginButton.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:20];
[loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[loginButton setTitle:#"Login" forState:UIControlStateNormal];
[cellView addSubview:loginButton];
}
} else {
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.text = [NSString stringWithFormat:#"Period %ld A+", (long)indexPath.row];
cell.detailTextLabel.text = #"Class name";
//TODO get grades and config using method (TB Created)
}
} else if (indexPath.section == 1) {
if (indexPath.row == 0) {
[self configureHeaderCell:cell title:#"Hourly Forecast"];
}
else {
// Get hourly weather and configure using method
MACondition *weather = [MAManager sharedManager].hourlyForecast[indexPath.row - 1];
[self configureHourlyCell:cell weather:weather];
}
}
else if (indexPath.section == 2) {
if (indexPath.row == 0) {
[self configureHeaderCell:cell title:#"Daily Forecast"];
}
else if (indexPath.section == 2) {
// Get daily weather and configure using method
MACondition *weather = [MAManager sharedManager].dailyForecast[indexPath.row - 1];
[self configureDailyCell:cell weather:weather];
}
}
return cell;
}
Implement the following UITableView Delegate Method
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//In here, check the index path. When you have the cell that contains the button, pop it out from there by using [button removeFromSuperView];
}
Your problem occurs when you dequeue the cell. Since the cell is being reused, it already has the button and you're simply re-adding it again. This will solve your issue. However, I'd recommend you create a subclass for the UITableViewCell, and in it's prepareForReuse method, pop the button out. Up to you. Both will work.
Table view cells are not just deallocated then they move out of visible area. They are stored for reusing and then returned in tableView dequeueReusableCellWithIdentifier:CellIdentifier];
So you need to clean your cells after using or before reusing. There are several ways:
1.Add tag to your button when you create it
loginButton.tag = SOME_TAG;
just after
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
search for view with this tag
loginButton = [cell viewWithTag:SOME_TAG];
if loginButton != nil you can reuse it or remove from cell and then create a new one.
2.Implement UITableViewDelegate method
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
and erase login button inside it.
3.Create custom UITableViewCellclass and implement prepareForReuse method.
You're adding the button every time you return a cell in this method. If you scroll the cell off the screen and back on, this method is called again for the same index path, and you will add the button again.
You declare the variable, do nothing with it, then check if it is nil. It will always be nil, so you always add the button.
A quick and dirty solution is to give the button a tag, then check for its existence using viewWithTag:.
A better solution is to make a custom cell subclass, and set one-time properties like this in the init method. Your cell contents seem very different for each section as well, so use different reuse identifiers for each section, and possibly a different cell subclass. Clearing out sub views is expensive and could hurt your scrolling performance.
When you run your project first time then cellForRowAtIndexPath is called.....
Then whenever you scroll tableView it again calls cellForRowAtIndexPath and reload data automatically..
So you have to take CellIdentifier as unique for each cell.
you have to remove static keyword from
static NSString *CellIdentifier = #"CellIdentifier";
now you have
NSString *CellIdentifier = #"CellIdentifier";
only this things
Now you have to write like below
NSString *CellIdentifier = [NSString stringWithFormat:#"%#",indexPath];
Now Enjoy.....

Set focus to UITextField in tableCell

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()
}
}

Resources