如何访问自定义单元格文本字段值Swift 3.0

我已经创build了一个自定义的tableViewCell类,它是一个持有文本字段的原型单元格。 在ThirteenthViewController里面,我想引用tableViewCell类,这样我就可以访问它的doorTextField属性,以便为其分配从UserDefaults返回的数据。 我怎样才能做到这一点?

 class ThirteenthViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate { var options = [ Item(name:"Doorman",selected: false), Item(name:"Lockbox",selected: false), Item(name:"Hidden-Key",selected: false), Item(name:"Other",selected: false) ] let noteCell:NotesFieldUITableViewCell! = nil @IBAction func nextButton(_ sender: Any) { //save the value of textfield when button is touched UserDefaults.standard.set(noteCell.doorTextField.text, forKey: textKey) //if doorTextField is not empty assign value to FullData guard let text = noteCell.doorTextField.text, text.isEmpty else { FullData.finalEntryInstructions = noteCell.doorTextField.text! return } FullData.finalEntryInstructions = "No" } override func viewDidLoad() { let index:IndexPath = IndexPath(row:4,section:0) let cell = tableView.cellForRow(at: index) as! NotesFieldUITableViewCell self.tableView.delegate = self self.tableView.dataSource = self cell.doorTextField.delegate = self } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return options.count } func numberOfSections(in tableView: UITableView) -> Int { return 1 } // configure the cell func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.row < 3 { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")! cell.textLabel?.text = options[indexPath.row].name return cell } else { let othercell = tableView.dequeueReusableCell(withIdentifier: "textField") as! NotesFieldUITableViewCell othercell.doorTextField.placeholder = "some text" return othercell } } }//end of class class NotesFieldUITableViewCell: UITableViewCell { @IBOutlet weak var doorTextField: UITextField! override func awakeFromNib() { super.awakeFromNib() } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } } 

为了访问你的单元格中的UITextField ,你需要知道UITableView哪一行包含你的单元格。 就你而言,我相信这一排总是第四名。 所以,你可以为该行创build一个IndexPath,然后你可以简单地做这样的事情:

 let ndx = IndexPath(row:3, section: 0) let cell = table.cellForRow(at:ndx) as! NotesFieldUITableViewCell let txt = cell.doorTextField.text 

以上可能不完全是语法正确,因为我没有检查语法,但我相信你可以从那里拿走,对吧?

但是,请注意,为了使上述工作,最后一行(第4行)必须始终可见。 如果您尝试获取不可见的行,则会遇到访问它们的问题,因为UITableView重复使用单元格并为可见的数据行实例化单元格。

另外,如果你只是简单地想要获取用户键入的文本和文本input,当他们点击“Enter”时,你总是可以简单地绕过访问表行,并添加一个UITextFieldDelegate到你的自定义单元格,以发送一个通知input的文本,以便您可以侦听通知,并采取一些行动。

但是,正如我上面提到的,这一切都取决于你如何设置和你正在努力实现的东西:)

更新:

基于进一步的信息,当nextButton方法被调用时,看起来好像你想用文本值做些什么。 如果是这样,下面应该(理论上)做你想要的:

 @IBAction func nextButton(_ sender: Any) { // Get the cell let ndx = IndexPath(row:4, section: 0) let cell = table.cellForRow(at:ndx) as! NotesFieldUITableViewCell //save the value of textfield when button is touched UserDefaults.standard.set(cell.doorTextField.text, forKey: textKey) //if doorTextField is not empty assign value to FullData guard let text = cell.doorTextField.text, text.isEmpty else { FullData.finalEntryInstructions = cell.doorTextField.text! return } FullData.finalEntryInstructions = "No" } 

您可以为doorTextField创build一个标签(例如111)现在您可以检索该值。

 @IBAction func nextButton(_ sender: Any) { //save the value of textfield when button is touched guard let textField = self.tableViewview.viewWithTag(111) as! UITextField? else { return } prit(textField.text) ..... 

}