移动到表格单元格中的下一个文本字段
-
08-09-2020 - |
题
在我的应用程序中,有一个表视图,每个单元格为2个文本字段。AM为每个文本字段分配标记。在按下完成按钮,我想制作下一个文本字段第一个响应者(可以在表格单元格之间)。但是现在我使用的代码不起作用。附加本代码
NSArray *viewsToBecomeFirstResponder = [tableView subviews];
for (UIView *firstResponder in viewsToBecomeFirstResponder) {
if ([firstResponder isKindOfClass:[UITextField class]]) {
if (firstResponder.tag == nextTag ) {
[firstResponder becomeFirstResponder];
}
}
}
.
我没有在子视图阵列中获取文本字段。我正在使用表格单元格的自定义单元格。请帮忙。
解决方案
对Karim的代码很少,您可以访问这样的帖子:
- (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];
}
.
希望这有帮助。
其他提示
定义如下所列的方法 -
- (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;
}
.
其他地方,
UITextField *nextField = [self nextToRespondAfterTextField:textField];
[nextField becomeFirstResponder];
. 由于您使用标记来标识您的TextView。另一种方法可以是使用文本查看委托。
- (void)textViewDidEndEditing:(UITextView *)textView {
int currentTag = textView.tag;
int nextTag = currentTag + 1; // or something else
UITextView *nextTV = [self.tableView viewWithTag: nextTag];
[nextTV becomesFirstResponder];
}
. 不隶属于 StackOverflow