표 셀의 다음 텍스트 필드로 이동합니다
-
08-09-2020 - |
문제
내 응용 프로그램에서는 셀 당 2 개의 텍스트 필드가있는 테이블 뷰가 있습니다.각 텍스트 필드에 태그를 할당하고 있습니다.DONE 버튼을 누르면 다음 텍스트 필드를 첫 번째 응답자 (테이블 셀 사이에있을 수 있음)를 만들고 싶습니다.그러나 이제 코드가 사용 중이 아닙니다.현재 코드를 첨부
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];
. 태그를 사용하여 텍스트보기를 식별하고 있기 때문에.또 다른 방법은 텍스트보기 대표를 사용할 수 있습니다.
- (void)textViewDidEndEditing:(UITextView *)textView {
int currentTag = textView.tag;
int nextTag = currentTag + 1; // or something else
UITextView *nextTV = [self.tableView viewWithTag: nextTag];
[nextTV becomesFirstResponder];
}
. 제휴하지 않습니다 StackOverflow