【问题标题】:How can I retrieve the data from a text field that is embedded in a UITableView in Swift?如何从 Swift 中嵌入 UITableView 的文本字段中检索数据?
【发布时间】:2018-01-31 22:40:42
【问题描述】:

我一直在尝试在 UITableView 中创建各种提交表单(即每行都有一个文本字段的表格视图),但无法从以编程方式生成的文本字段中检索用户输入在表格视图中。

这是目前为止的代码:

class VC12: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {

@IBOutlet weak var VC12TableView: UITableView!

var VC12Inputs = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    self.VC12TableView.dataSource = self

    VC12Inputs.append("Input 1")
    VC12Inputs.append("Input 2")
    VC12Inputs.append("Input 3")
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return VC12Inputs.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = VC12TableView.dequeueReusableCell(withIdentifier: "cell") ?? makeCell()
    let textField = cell.viewWithTag(1) as! UITextField
    let row = indexPath.row

    textField.placeholder = VC12Inputs[row]

    return cell
}

func makeCell() -> UITableViewCell {
    let textField = UITextField(frame: CGRect.zero)

    textField.tag = 1
    [textField].forEach {
        $0.translatesAutoresizingMaskIntoConstraints = false
        $0.borderStyle = .roundedRect
    }

    let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    [textField].forEach { cell.contentView.addSubview($0) }

    let views = ["textField": textField]
    let c1 = NSLayoutConstraint.constraints(withVisualFormat: "H:[textField]", options: .alignAllLastBaseline, metrics: nil, views: views)
    let c2 = NSLayoutConstraint(item: textField, attribute: .left, relatedBy: .equal, toItem: cell.layoutMarginsGuide, attribute: .left, multiplier: 1, constant: 0)
    let c3 = NSLayoutConstraint(item: textField, attribute: .centerY, relatedBy: .equal, toItem: cell.contentView, attribute: .centerY, multiplier: 1, constant: 0)
    let c4 = NSLayoutConstraint(item: textField, attribute: .width, relatedBy: .equal, toItem: cell.contentView, attribute: .width, multiplier: 0.9, constant: 0)
    let c5 = NSLayoutConstraint(item: textField, attribute: .height, relatedBy: .equal, toItem: cell.contentView, attribute: .width, multiplier: 0.09, constant: 0)

    NSLayoutConstraint.activate(c1 + [c2, c3, c4, c5])

    return cell
}

@IBAction func VC9Done(_ sender: Any)
{

}

这会生成一个带有 3 个文本字段的标准表格视图;每行一个,即像这样:https://i.stack.imgur.com/JOwO0.png

但是,当用户点击“完成”时,我不确定如何检索用户输入到文本字段中的文本。对解决此问题的最佳方法有何想法?

此处显示的代码基于 Code Different (https://stackoverflow.com/users/2538939/code-different?tab=profile) 与 B B (https://stackoverflow.com/users/6155520/b-b) 的问题 (Adding a text field to a UITableview Cell) 的 Stack Overflow 答案。

【问题讨论】:

    标签: ios arrays swift uitableview uitextfield


    【解决方案1】:

    你可以试试这个

    class VC12: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {
    
    @IBOutlet weak var VC12TableView: UITableView!
    
    var VC12Inputs = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.VC12TableView.dataSource = self
    
        VC12Inputs.append("Input 1")
        VC12Inputs.append("Input 2")
        VC12Inputs.append("Input 3")
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        VC12Inputs[textField.tag] = textField.text! //to update arr with latest value 
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return VC12Inputs.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = VC12TableView.dequeueReusableCell(withIdentifier: "cell") ?? makeCell()
        let textField = cell.viewWithTag(1) as! UITextField
        let row = indexPath.row
        textField.tag = row      //**set tag value here** 
        textField.delegate = self   //**set delegate**
    
        textField.placeholder = VC12Inputs[row]
    
        return cell
    }
    
    func makeCell() -> UITableViewCell {
        let textField = UITextField(frame: CGRect.zero)
    
        textField.tag = 1 //don't set tag value here or set with indexpath.item
        . .....
        .....
    
    }
    
    @IBAction func VC9Done(_ sender: Any)
    {
        print(VC12Inputs) //here you'll get all updated value at any time 
    }
    

    【讨论】:

      【解决方案2】:

      我将创建一个自定义类单元格,并在该类中进行UITextField 初始化(为其分配一个变量名)。

      class TextFieldCell: UITableViewCell {
          let textField = // initialize text field
      
          // don't forget to add its constraints and init functions
      }
      

      然后在您的VC9done 函数中,您可以调用表格视图的cellForRow 函数并获取包含所需文本字段的单元格。获得此单元格后,您可以将其转换为自定义类,然后访问其 .textField 字段及其包含的任何文本。

      【讨论】:

      • 当文本字段中的文本发生变化时,您还需要一个委托模式来更新您的数据模型;如果 tableview 滚动并且单元格被重用,如果你不这样做,你将丢失任何更改。
      • 我喜欢在故事板中布置 UI,这样我就可以直观地设置约束。这也让我可以从文本字段控制拖动到我的视图控制器以创建一个 IBOutlet。这一切都避免了必须键入强制转换之类的东西。
      【解决方案3】:

      借助UITextFieldDelegate方法,我们可以实现它。

      var textFieldTextDict = [Int : String]()
      
      func textFieldDidEndEditing(_ textField: UITextField) {
      
          let cellPosition = textField.superview?.convert(CGPoint.zero, to: yourTableView)
          let indPath = yourTableView.indexPathForRow(at: cellPosition!)
      
          if textField.text != ""
          {
              textFieldTextDict[(indPath?.row)!] = textField.text
          }      
      
      }
      
      func textFieldShouldReturn(_ textField: UITextField) -> Bool {
      
          textField.resignFirstResponder()
      
          return true
      }
      
      @IBAction func VC9Done(_ sender: Any)
      {
          self.view.endEditing(true) // ONE LINE
      
          print("addedValues   ", textFieldTextDict)
      }
      
      @IBAction func formSubmitAcn(_ sender: Any)
      {
          // as per three textfields
      
          if textFieldTextDict.count == 3
          {
              print("success")
          }
          else 
          {
              print("failure")
          }
      }
      

      输出

      addedValues   [0: "name ", 2: "age", 3: "address", 1: "email", 4: "phone"]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多