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

时间:2017-03-27 02:09:26

标签: ios swift uitableview

我为一个持有文本字段的原型单元创建了一个自定义的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)
   }
}

2 个答案:

答案 0 :(得分:6)

要访问单元格中的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"
}

答案 1 :(得分:1)

您可以为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)

.....

}

相关问题