2012-01-16 9 views
24

Tôi đang tạo chế độ xem bảng với UITextField s động.Đặt tiêu điểm thành UITextField trong tableCell

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]; 

Và bây giờ tôi muốn đặt trọng tâm vào trường văn bản này khi người dùng chạm vào ô. Tôi viết những dòng này

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

Nhưng điều này không làm việc

Trả lời

59

thay đổi [field resignFirstResponder]; với [field becomeFirstResponder];

resignFirstResponder được sử dụng để loại bỏ bàn phím (tập trung) từ trường văn bản

+0

Không có gì happend, tôi nghĩ nó như thế vì tôi thiết tblView.allowsSelection = NO; Và sự kiện này không vui vẻ. Nhưng nếu tôi đặt tblView.allowsSelection = YES; Nó chọn ô chỉ = ( – nabiullinas

+0

Tôi cố gắng thực hiện cách tiếp cận như vậy tuy nhiên điều này không hoạt động bình thường Cố gắng gán UITextField cho [textField trở thànhFirstResponder] phải đặt lựa chọn được bật hoặc được đặt thành YES/true. –

1

tôi đã phải đối mặt với cùng một vấn đề ở đây, thậm chí sử dụng đúng cách [field becomeFirstResponder];.

Tôi cũng đã sử dụng thẻ để lấy các đối tượng tôi có trong ô. didSelectRowAtIndexPath của bạn sẽ trông giống như thế này để có được ô bên phải:

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

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

Nó hoạt động như một sự quyến rũ đối với tôi.

0

Swift 3

myTextField.becomeFirstResponder() 

Mã của tôi. Đi theo đường dẫn chỉ mục của ô hiện tại. Và lấy ô của indexpath.row tiếp theo. Và gọi ** 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() 
     } 
}