问题
In my application , there is a table view with 2 text fields per cell. Am assigning tags for each text field. On pressing done button i want to make the next text field the first responder(can be between table cells). But the code now i am using does not work . attaching the present code
NSArray *viewsToBecomeFirstResponder = [tableView subviews];
for (UIView *firstResponder in viewsToBecomeFirstResponder) {
if ([firstResponder isKindOfClass:[UITextField class]]) {
if (firstResponder.tag == nextTag ) {
[firstResponder becomeFirstResponder];
}
}
}
I am not getting the text fields in the sub view array. I am using custom cell for table cells. Please help.
回答1:
With little modification to Karim's code you can access the textVies like this:
- (void)textViewDidEndEditing:(UITextView *)textView {
//Get the indexPath for the currently selected cell
NSIndexPath *cellPath = [(UITableView*)[self view] indexPathForSelectedRow];
//Get the actual cell
CustomTableCell *cell = (CustomTableCell *)[(UITableView*)[self view] cellForRowAtIndexPath:cellPath];
int currentTag = textView.tag;
int nextTag = currentTag + 1; // or something else
UITextView *nextTV = [cell viewWithTag: nextTag];
[nextTV becomesFirstResponder];
}
Hope this helps.
回答2:
Define a method as listed below –
- (UITextField *) nextToRespondAfterTextField:(UITextField*)textField {
NSInteger nextTag = textField.tag + 1;
CustomTableCell *theCell = (CustomTableCell*)[textField.superview];
for ( UIView *aView in theCell.subviews ) {
if ( [aView isKindOfClass:[UITextField class]]) {
if ( aView.tag == nextTag ) {
return aView;
}
}
}
NSIndexPath *indexPath = [self.tableView indexPathForCell:theCell];
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
NSInteger numberOfSections = [self.tableView numberOfSections];
if ( [self.tableView numberOfRowsInSection:section] != (row + 1) ) {
indexPath = [NSIndexPath indexPathForRow:(row + 1) inSection:section];
} else {
do {
section++;
} while ( section < numberOfSections && [self.tableView numberOfRowsInSection:section] == 0 ) ;
if ( section == numberOfSections ) {
return nil;
} else {
indexPath = [NSIndexPath indexPathForRow:0 inSection:section];
}
}
theCell = (CustomTableCell*)[self.tableView cellForRowAtIndexPath:indexPath];
if ( theCell == nil ) {
return nil;
}
for ( UIView *aView in theCell.subviews ) {
if ( [aView isKindOfClass:[UITextField class]]) {
if ( aView.tag == nextTag ) {
return aView;
}
}
}
return nil;
}
Elsewhere,
UITextField *nextField = [self nextToRespondAfterTextField:textField];
[nextField becomeFirstResponder];
回答3:
Since you are using tag to identify your textview. Another way can be to use text view delegates.
- (void)textViewDidEndEditing:(UITextView *)textView {
int currentTag = textView.tag;
int nextTag = currentTag + 1; // or something else
UITextView *nextTV = [self.tableView viewWithTag: nextTag];
[nextTV becomesFirstResponder];
}
来源:https://stackoverflow.com/questions/6081325/to-move-to-next-text-fieds-in-table-cell