2012-01-16 2 views
24

UITextField의 테이블 뷰를 동적으로 생성합니다.테이블 셀의 UITextField에 포커스 설정

l_textField = [[UITextField alloc] initWithFrame:TextFieldFrame]; 
l_textField.tag = cellRow; 

l_textField.delegate = self; 
l_textField.font = [UIFont boldSystemFontOfSize:14]; 
l_textField.textColor = [UIColor blackColor]; 
[l_textField setEnabled:YES]; 

[cell.contentView addSubview:l_textField]; 

이제 사용자가 터치 셀을 사용할 때이 텍스트 필드에 중점을두고 싶습니다. 나는이

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath { 
    UITextField* field = (UITextField *) [tblView viewWithTag: newIndexPath.row]; 
    [field resignFirstResponder]; 
} 

쓰기하지만 내가 같은 직면 한 텍스트 필드에서

답변

59

변화 [field resignFirstResponder]; 작동하지 않습니다 [field becomeFirstResponder];을 올바르게 사용해도 여기에서 문제가 발생합니다.

또한 태그를 사용하여 셀 안에있는 객체를 가져 왔습니다. 귀하의 didSelectRowAtIndexPath 오른쪽 셀을 얻으려면 다음과 같이해야합니다 : 그것은 나를 위해 마법처럼 일했다

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    currentRow = indexPath.row; 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    UITextField *indexField = (UITextField *)[cell viewWithTag:101]; 
    [indexField becomeFirstResponder]; 
} 

.

+0

아무것도 앞으로 일어날라고 내가 설정 렸기 때문에, 나는 그런 식으로 생각 tblView.allowsSelection = NO; 그리고이 사건은 일어나지 않습니다. 하지만 tblView.allowsSelection = YES로 설정하면; 그것은 셀만 선택 = ( – nabiullinas

+0

그런 접근 방식을 구현하려했으나 일반적으로 작동하지 않습니다. [textField becomeFirstResponder]에 UITextField를 할당하려고하면 선택 항목이 활성화되거나 YES/true로 설정되어야합니다. –

1

를 키보드 (초점)를 제거하는 데 사용됩니다 [field becomeFirstResponder];

resignFirstResponder

0

스위프트 3

myTextField.becomeFirstResponder() 

내 코드입니다. 현재 셀의 인덱스 경로를 가져옵니다. 그리고 다음 indexpath.row의 세포를 얻으십시오. 그리고 ** cell1.txtFindings.becomeFirstResponder() **

if let textFieldIndexPath = specsListView.indexPath(for: cell){ 
     let newIndexPath = IndexPath(row: textFieldIndexPath.row + 1, section: 0) 

     if let cell1: SpecsTableViewCell = specsListView.cellForRow(at: newIndexPath) as? SpecsTableViewCell{ 
      cell1.txtFindings.becomeFirstResponder() 
     } 
}