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

相关问题