My tableview cell contain one label and one textfield, I am reusing my cell and it is working. but when I enter text in first textfield and hit return I want to go to next textfield, how can I handle that?
Asked
Active
Viewed 699 times
0
-
1make your next textfield as first responder after return key press of first textfield – johny kumar Sep 10 '15 at 05:53
-
ref this http://stackoverflow.com/questions/4375442/accessing-uitextfield-in-a-custom-uitableviewcell – Anbu.Karthik Sep 10 '15 at 06:08
3 Answers
1
Here's what I did:
Created a delegate protocol with these methods:
- (void)textFieldInCellDidReturn:(UITableViewCell*)cell; - (void)textFieldInCellDidBeginEditing:(UITableViewCell*)cell; - (void)textFieldInCellDidEndEditing:(UITableViewCell*)cell;
In the cell:
-(BOOL)becomeFirstResponder { return [self.inputTextField becomeFirstResponder]; } -(void)textFieldDidBeginEditing:(UITextField *)textField { if ([self.delegate respondsToSelector:@selector(textFieldInCellDidBeginEditing:)]) { [self.delegate textFieldInCellDidBeginEditing:self]; } } -(void)textFieldDidEndEditing:(UITextField *)textField { if ([self.delegate respondsToSelector:@selector(textFieldInCellDidEndEditing:)]) { [self.delegate textFieldInCellDidEndEditing:self]; } } -(BOOL)textFieldShouldReturn:(UITextField *)textField { if ([self.delegate respondsToSelector:@selector(textFieldInCellDidReturn:)]) { [self.delegate textFieldInCellDidReturn:self]; } return NO; }
In the view controller and table view delegate, declare the iVar
NSIndexPath* indexPathOfFirstResponder
and adopt the delegate- (void)textFieldInCellDidBeginEditing:(UITableViewCell *)cell { indexPathOfFirstResponder = [self.theTable indexPathForCell:cell]; } -(void)textFieldInCellDidReturn:(UITableViewCell *)cell { NSIndexPath* indexPathForCell = [self.theTable indexPathForCell:cell]; if(indexPathForCell.row < [self.theTable numberOfRowsInSection:indexPathForCell.section]-1) { NSIndexPath* nextIndexPathInSection = [NSIndexPath indexPathForRow:indexPathForCell.row+1 inSection:indexPathForCell.section]; UITableViewCell* nextCellInSection = [self.theTable cellForRowAtIndexPath:nextIndexPathInSection]; if (nextCellInSection) { [nextCellInSection becomeFirstResponder]; } else { indexPathOfFirstResponder = nextIndexPathInSection; [self.theTable scrollToRowAtIndexPath:nextIndexPathInSection atScrollPosition:(UITableViewScrollPositionMiddle) animated:YES]; } } else { [self.theTable endEditing:YES]; } } -(void)textFieldInCellDidEndEditing:(UITableViewCell *)cell { indexPathOfFirstResponder = nil; } -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([indexPath isEqual:indexPathOfFirstResponder]) { [cell becomeFirstResponder]; } }

Ahmed Hamed
- 504
- 1
- 3
- 11
0
Set sequential tag for the textField e.g. 0,1,2..... Import textField delegate method textFieldDidEndEditing. Whenever return key is pressed, this method gets called.
Use something like this:
-(void)textFieldDidEndEditing:(UITextField *)textField{
if(textField.tag == currentTextFieldTag+1){
[textField becomesFirstResponder];
}
}

Vikas Mishra
- 246
- 1
- 12
0
in your cell for row function use this code
cell.textField.tag = indexPath.row;
Now implement a UITextFieldDelegate
Method
-(void)textFieldDidEndEditing:(UITextField *)textField
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag+1 inSection:YOUR_SECTION];
UITableViewCell* cell = [yourTable cellForRowAtIndexPath:indexPath];
[cell.textField becomesFirstResponder];
// if you need [yourTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
I hope this will help.Thankyou

baydi
- 1,003
- 6
- 11