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

bib*_*scy 1 uitableview ios swift

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

Fah*_*him 6

要访问UITextField单元格,您需要知道UITableView包含单元格的哪一行.在你的情况下,我相信行总是第四行.因此,您可以为该行创建一个IndexPath,然后,您可以简单地执行以下操作:

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

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

但是,请注意,为了使上述功能正常工作,最后一行(第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"
}
Run Code Online (Sandbox Code Playgroud)