gpt4 book ai didi

ios - 自定义单元格中的焦点 UITextfield

转载 作者:行者123 更新时间:2023-11-28 06:19:24 27 4
gpt4 key购买 nike

我在自定义单元格中有 2 个 UITextfield。现在我想在从其他 View 推送时将焦点放在第一个文本字段上。

这是我在 cellForRowAt 委托(delegate)中的代码,但这不起作用:

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
}

最佳答案

问题是,您有多个单元格,并且每次绘制时都会指示所有单元格 becomeFirstResponder

如果您只想在显示表格时将焦点设置在第一个单元格上,并且只有在那时,您可以在 indexPath.row == 0 时标记 UITextFieldviewDidAppear 中,您只需获取带有标签的字段并设置 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)
}

关于ios - 自定义单元格中的焦点 UITextfield,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44139827/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com