简体   繁体   English

如何使用UITableView for TextField输入视图[Swift]

[英]How to use UITableView for TextField input view [Swift]

I currently have an app which uses UIPickerViews to allow users to select which answer they want for a text field (to avoid spelling mistakes etc). 我目前有一个应用程序,它使用UIPickerViews允许用户选择他们想要的文本字段的答案(以避免拼写错误等)。

使用UIPickerView的表单截图

However, I have found that the UIPickerView isn't really what I want to use because I haven't had great feedback from it when testing. 但是,我发现UIPickerView并不是我想要使用的,因为我在测试时没有得到很好的反馈。

I have done some research into how to use a UITableView for text field inputs instead, so when the user clicks the Textfield, the user segues to a UITableView with the same options which would be provided by the UIPickerView. 我已经对如何使用UITableView进行文本字段输入进行了一些研究,因此当用户单击Textfield时,用户将使用UIPickerView提供的相同选项切换到UITableView。 Once they click the cell with the option they are looking for it would segue back to the form with the result chosen inside the text field. 一旦他们点击具有他们正在寻找的选项的单元格,它将返回到在文本字段内选择结果的表单。 I thought this would be a better user experience as I could also implement the search to help users narrow down the option they require quicker. 我认为这将是一个更好的用户体验,因为我还可以实现搜索,以帮助用户缩小他们需要更快的选项。

I have been trying to get this to work for a while now, but I'm quite new at coding and haven't been able to crack it yet. 我一直试图让它工作一段时间,但我在编码方面很陌生并且还没能破解它。 I would just like advice on how to approach this? 我想就如何处理此问题提出建议? I'm using Swift and the Storyboard to create my app. 我正在使用Swift和Storyboard来创建我的应用程序。

Would I need to create a separate ViewController with a UITableView that loads the options and then move the value back to the form once the cell is clicked? 我是否需要使用UITableView创建一个单独的ViewController来加载选项,然后在单击单元格后将值移回到表单中?

Thank you, I appreciate any help given. 谢谢,我感谢任何帮助。

One approach would be to use a table view in separate view controller. 一种方法是在单独的视图控制器中使用表视图。 lets call it choiceVC and pass data which text field was tapped. 我们称之为choiceVC并传递文本字段被点击的数据。 Then send the data back to your form to show what user has selected. 然后将数据发送回表单以显示用户选择的内容。

Follow these steps 跟着这些步骤

Detect user tap on text field and segue to choiceVC by implementing this delegate function of UITextField 检测用户点击文本字段并通过实现UITextField的委托功能来segue到choiceVC

func textFieldShouldReturn(_ textField: UITextField) -> Bool {

  textField.resignFirstResponder()
  //push your choiceVC and pass data
  var textFeildTapped = ""
  //note: chooseGameTextField should be text field's outlet
  if textField == chooseGameTextField{
    textFeildTapped = "games"
  }else if textField == chooseActiviyTextField {
    textFeildTapped = "activiy"
  } 

//first set identifier of your view controller by going to identity inspector and setting the value StoryBoard ID
if let controller = storyboard?.instantiateViewController(withIdentifier: "choiceVC") as? ProductDetailVc{
  //pass which text field was tapped
  controller.choicesToShow = textFeildTapped
  navigationController?.pushViewController(controller, animated: true)
}
return true

} }

Note: choiceVC should have a variable "choicesToShow" of type string 注意:choiceVC应该有一个string类型的变量“choicesToShow”

in viewdidload of choiceVC check the variable 在viewdidload of choiceVC中检查变量

if choicesToShow == "games" {
 //populate your table view with games. 
}else {
 //check for other cases and proceed accordingly
 //activiy, console etc
}

Implement didSelect delegate method of UITableView 实现UITableView的didSelect委托方法

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 //Pass data back to your form using **delegate pattern**. see link in notes below
}

Notes: 笔记:

I hope this code work for you. 我希望这段代码适合你。 it's wroking for me. 它对我来说很吵。

First view controller for textfiled form where you want to open tableview.for that use textfield Delegate. 第一个视图控制器,用于打开使用textfield Delegate的tableview.for的textfiled表单。

First View Controller 第一视图控制器

 func doSomething(text: UITextField, with data: String) {
            text.text = data
        }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        let objGametableVC = self.storyboard?.instantiateViewController(withIdentifier: "GametableVC") as! GametableVC;
        objGametableVC.delegate = self
        objGametableVC.selectedTextField = textField
        if textField == txtActivity{
            objGametableVC.tblData.removeAll()
            objGametableVC.tblData = ["act1","act2"]
        }
        else if textField == txtGameName{
            objGametableVC.tblData.removeAll()
            objGametableVC.tblData = ["gam1","game2"]
        }
        textField.resignFirstResponder()
        self.navigationController?.pushViewController(objGametableVC, animated: true);
    }

Second view Controller For tableview show and pass data from second to first controller 第二个视图控制器用于tableview显示并将数据从第二个控制器传递到第一个控

Second view Controller 第二视图控制器

var delegate: Delegate? var delegate:Delegate?

var tblData: [String] = String var tblData:[String] = String

var selectedTextField:UITextField? var selectedTextField:UITextField? = nil =没有

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let tblcell = tableView.dequeueReusableCell(withIdentifier: "gamelblCell", for: indexPath) as! gamelblCell
        tblcell.lblName.text = tblData[indexPath.row]
        return tblcell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let tblcell = tableView.cellForRow(at: indexPath) as! gamelblCell
        let data = tblcell.lblName.text
        delegate?.doSomething(text: selectedTextField!, with: data!)
        self.navigationController?.popViewController(animated: true)
    }

If you are looking for an alternative for picker view to select options you can use dropdown like controls Eg. 如果您正在寻找选择器视图的替代方案来选择选项,您可以使用下拉类似控件例如。

  1. DropDown 落下
  2. RSSelectionMenu RSSelectionMenu

I hope these libraries can solve your issues, best of luck 我希望这些图书馆可以解决您的问题,祝您好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM