2017-05-23 2 views
1

사용자 지정 셀에 2 UITextfield이 있습니다. 이제 다른 뷰에서 푸시 할 때이 첫 번째 텍스트 필드에 중점을두고 싶습니다. 여기 사용자 지정 셀의 포커스 UITextfield

cellForRowAt 위임에 내 코드하지만이 작동하지 않습니다

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
let userCell = tableView.dequeueReusableCell(withIdentifier: "InputUserInfoForm") as! InputUserInfoForm 
     userCell.selectionStyle = .none 

     userCell.fistnameTextField.autocorrectionType = .no 
     userCell.lastnameTextField.autocorrectionType = .no 

     // Firstname textfield 
     userCell.fistnameTextField.tag = RegisterForm.Firstname.rawValue 
     userCell.firstnameLabel.text = "FirstnameTitle".localized() 
     userCell.firstnameLabel.becomeFirstResponder() 

     // Lastname textfield 
     userCell.lastnameTextField.tag = RegisterForm.Lastname.rawValue 
     userCell.lastnameLabel.text = "LastnameTitle".localized() 

     return userCell 
} 
+0

왜이 코드를'cellForRowAt'에 넣고 있습니까? 'viewDidAppear'에 더 적합합니다. – toddg

답변

1

것은 당신이 여러 셀을 가지고 있으며, 그들 모두가 becomeFirstResponder마다에 지시 그들은 그려진다.

당신은 표가 표시됩니다 만 다음, 당신이 태그를 추가 할 수 있습니다 경우에만 첫 번째 셀에 포커스를 설정하려면 그 UITextFieldindexPath.row == 0viewDidAppear 당신은 간단한 태그와 해당 필드를 얻고 becomeFirstResponder

설정
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
let userCell = tableView.dequeueReusableCell(withIdentifier: "InputUserInfoForm") as! InputUserInfoForm 
    userCell.selectionStyle = .none 

    userCell.fistnameTextField.autocorrectionType = .no 
    userCell.lastnameTextField.autocorrectionType = .no 

    // Firstname textfield 
    userCell.fistnameTextField.tag = RegisterForm.Firstname.rawValue 
    userCell.firstnameLabel.text = "FirstnameTitle".localized() 
    if indexPath.row == 0 { 
     userCell.firstnameLabel.tag = 123 
    } 

    // Lastname textfield 
    userCell.lastnameTextField.tag = RegisterForm.Lastname.rawValue 
    userCell.lastnameLabel.text = "LastnameTitle".localized() 

    return userCell 
} 

override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(animated) 

    self.view.viewWithTag(123) 
} 
+0

이제 괜찮습니다. 감사합니다 :) –

+0

@ ManhNguyen 나는 당신이 stackoverflow에 새로운 것을 참조하십시오. 이 답이 문제를 해결했다면 그것을 수락 한 것으로 표시하는 것을 잊지 마십시오. 감사. – thedp

관련 문제