【问题标题】:Getting all the textField value from tableView into Array and Validation将 tableView 中的所有 textField 值获取到数组和验证中
【发布时间】:2019-05-20 04:38:02
【问题描述】:

我有两个具有相似单元格设计的 tableView。单元格名称和年龄中有两个文本字段。图片中可以看到

现在用户将数据填充到每个文本字段中。然后在continue 按钮上,我想将所有名称附加到数组中。与时代相似。

并且还想对空白值进行验证。如果任何文本字段为空白,则Continue 按钮上的警报将弹出。

【问题讨论】:

  • 为什么有两个tableview?使用一个 tableview 和两个部分

标签: arrays swift uitableview uitextfield


【解决方案1】:

您可以通过cellForRow(at:)方法获取tableview的单元格,然后访问名称标签或年龄标签,如下所示,

let indexPath = IndexPath(row: 0, section: 0)
if let cell = yourTableView.cellForRow(at: indexPath) as? yourTableViewCell {
    let name = cell.nameLabel.text
    ...
}

【讨论】:

    【解决方案2】:

    需要在ViewController 中实现UITextFieldDelegate 并实现TextFieldEndEditing 方法并将编辑的文本字段文本保存在字符串数组中。

    请参阅以下链接以获取所有文本字段值。

    https://phrkrish.wordpress.com/2016/12/25/table-view-with-text-field-in-swift-3-0/

    【讨论】:

      【解决方案3】:

      不要使用两个表视图,而是使用一个包含两个部分的表视图。并且不要使用 dequeueReusableCell。创建两个 UITableViewCell 对象数组。并在继续按钮操作中从数组对象中获取文本字段值。

      //CustomCell.swift

      class CustomCell: UITableViewCell {
          let titleLbl = UILabel()
          let seatNoLbl = UILabel()
          let nameTxtField = UITextField()
          let ageTxtField = UITextField()
          //etc
      }
      

      //ViewController.swift

      class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
      
          let tableView = UITableView()
          var onwardSeatNos = ["L1","L15"]
          var returnSeatNos = ["L2","L3"]
          var onwardSeatCells = [CustomCell]()
          var returnSeatCells = [CustomCell]()
          override func viewDidLoad() {
              super.viewDidLoad()
              view.backgroundColor = .white
      
              onwardSeatNos.enumerated().forEach { (index,seatNo) in
                  let cell = CustomCell()
                  print(index == 0 ? "Primary Passenger" : String(format: "Co-passenger %02d", index))
                  cell.titleLbl.text = index == 0 ? "Primary Passenger" : String(format: "Co-passenger %02d", index)
                  cell.seatNoLbl.text = "Seat No. \(seatNo)"
                  onwardSeatCells.append(cell)
              }
              returnSeatNos.enumerated().forEach { (index,seatNo) in
                  let cell = CustomCell()
                  cell.titleLbl.text = index == 0 ? "Primary Passenger" : String(format: "Co-passenger %02d", index)
                  cell.seatNoLbl.text = "Seat No. \(seatNo)"
                  returnSeatCells.append(cell)
              }
          }
          func numberOfSections(in tableView: UITableView) -> Int {
              return returnSeatNos.isEmpty ? 1 : 2
          }
          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              if section == 0 {
                  return onwardSeatNos.count
              } else {
                  return returnSeatNos.count
              }
          }
          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              if indexPath.section == 0 {
                  return onwardSeatCells[indexPath.row]
              } else {
                  return returnSeatCells[indexPath.row]
              }
          }
          func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
              if section == 0 {
                  return "Passenger Information (Onward)"
              } else {
                  return "Passenger Information (Return)"
              }
          }
          @objc func confirmBtnAction(_ sender: UIButton) {
              if let index = onwardSeatCells.firstIndex(where: { $0.nameTxtField.text!.isEmpty }) {
                  print("Onward " + (index == 0 ? "Primary Passenger" : String(format: "Co-passenger %02d", index)) + " name is empty")
              } else if let index = onwardSeatCells.firstIndex(where: { $0.nameTxtField.text!.isEmpty }) {
                  print("Onward " + (index == 0 ? "Primary Passenger" : String(format: "Co-passenger %02d", index)) + " age is empty")
              } else if let index = returnSeatCells.firstIndex(where: { $0.nameTxtField.text!.isEmpty }) {
                  print("Return " + (index == 0 ? "Primary Passenger" : String(format: "Co-passenger %02d", index)) + " name is empty")
              } else if let index = returnSeatCells.firstIndex(where: { $0.nameTxtField.text!.isEmpty }) {
                  print("Return " + (index == 0 ? "Primary Passenger" : String(format: "Co-passenger %02d", index)) + " age is empty")
              } else {
                  let onwardNamesArr = onwardSeatCells.map { $0.nameTxtField.text! }
                  let onwardAgesArr = onwardSeatCells.map { $0.ageTxtField.text! }
                  let returnNamesArr = returnSeatCells.map { $0.nameTxtField.text! }
                  let returnAgesArr = returnSeatCells.map { $0.ageTxtField.text! }
              }
          }
      }
      

      【讨论】:

        【解决方案4】:
        func getAllTextFieldText(tableView: UITableView) {
            for i in 0...6 { // Depending on how many fields you need
                let indexPath = IndexPath(row: i, section: 0)
                if let cell = tableView.cellForRow(at: indexPath) as? yourCell {
                    if let values = cell.textField.text {
                        // *values* are all the text from each textField
                    }
                }
            }
        }
        

        继续@Michael Wang 的回答,对于多个文本字段,您可以简单地运行一个 for 循环来获取您需要的所有值并将它们添加到数组或您想要的任何内容中。我希望这对将来的其他人有所帮助。

        【讨论】:

          猜你喜欢
          • 2019-05-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-11
          • 2020-03-20
          • 1970-01-01
          • 2015-08-18
          相关资源
          最近更新 更多