2017-05-23 69 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

相關問題