Pour passer au champ de texte suivant dans la cellule du tableau
-
08-09-2020 - |
Question
Dans mon application, il y a une vue de table avec 2 champs de texte par cellule.AMP Attribuez des balises pour chaque champ de texte.En appuyant sur le bouton Terminé, je souhaite effectuer le champ de texte suivant le premier répondeur (peut être entre les cellules du tableau).Mais le code maintenant que j'utilise ne fonctionne pas.attacher le code actuel
NSArray *viewsToBecomeFirstResponder = [tableView subviews];
for (UIView *firstResponder in viewsToBecomeFirstResponder) {
if ([firstResponder isKindOfClass:[UITextField class]]) {
if (firstResponder.tag == nextTag ) {
[firstResponder becomeFirstResponder];
}
}
}
Je ne reçois pas les champs de texte dans le tableau Sous-View.J'utilise une cellule personnalisée pour les cellules de table.S'il vous plaît aider.
La solution
Avec peu de modification au code de Karim, vous pouvez accéder à la TextVies comme celle-ci:
- (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];
}
J'espère que cela aide.
Autres conseils
Définir une méthode énumérée ci-dessous -
- (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;
}
ailleurs,
UITextField *nextField = [self nextToRespondAfterTextField:textField];
[nextField becomeFirstResponder];
Puisque vous utilisez une balise pour identifier votre TextView.Une autre façon peut être d'utiliser des délégués de vue texte.
- (void)textViewDidEndEditing:(UITextView *)textView {
int currentTag = textView.tag;
int nextTag = currentTag + 1; // or something else
UITextView *nextTV = [self.tableView viewWithTag: nextTag];
[nextTV becomesFirstResponder];
}