[英]How to access custom cell textfield values Swift 3.0
我为保存文本字段的原型单元格创建了一个自定义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
哪一行包含您的单元格。 就您而言,我相信该行始终是第四行。 因此,您可以为该行创建一个IndexPath,然后可以简单地执行以下操作:
let ndx = IndexPath(row:3, section: 0)
let cell = table.cellForRow(at:ndx) as! NotesFieldUITableViewCell
let txt = cell.doorTextField.text
由于我没有检查语法,因此上述语法在语法上可能并不完全正确,但是我确定您可以从那里接受它,对吗?
但是,请注意,为了使上述内容起作用,最后一行(第4行)必须始终可见。 如果尝试获取不可见的行,则访问时会遇到问题,因为UITableView
重复使用单元格并为可见的数据行实例化单元格。
另外,如果您只想获取用户键入的文本,并且在他们点击“ Enter”时输入文本结束,则始终可以简单地完全绕过访问表行并将UITextFieldDelegate
添加到您的自定义单元格,以通过输入的文本,以便您可以收听通知并采取一些措施。
但是,正如我上面提到的,这一切都取决于您如何进行设置以及您要实现的目标:)
更新:
根据更多信息,似乎在调用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创建一个标签(例如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)
.....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.