如何将 UITableView 用于 TextField 输入视图

How to use UITableView for TextField input view

我目前有一个应用程序使用 UIPickerViews 允许用户select他们想要的文本字段答案(以避免拼写错误等)。

但是,我发现 UIPickerView 并不是我真正想要使用的,因为我在测试时没有得到很好的反馈。

我已经对如何使用 UITableView 进行文本字段输入进行了一些研究,因此当用户单击文本字段时,用户会转至具有与 UIPickerView 提供的相同选项的 UITableView。一旦他们单击带有他们正在寻找的选项的单元格,它将返回到带有在文本字段中选择的结果的表单。我认为这将是一个更好的用户体验,因为我还可以实现搜索以帮助用户更快地缩小他们需要的选项。

一段时间以来,我一直在努力让它发挥作用,但我对编码还很陌生,还没有破解它。我只想就如何处理这个问题提出建议?我正在使用 Swift 和情节提要来创建我的应用程序。

我是否需要创建一个单独的 ViewController 和一个加载选项的 UITableView,然后在单击单元格后将值移回表单?

一种方法是在单独的视图控制器中使用 table 视图。让我们称之为 choiceVC 并传递点击文本字段的数据。 然后将数据发送回您的表单以显示用户选择的内容。

按照以下步骤操作

检测用户点击文本字段并通过实现 UITextField

的委托函数转至 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

}

注意:choiceVC应该有一个字符串类型"choicesToShow"的变量

在viewdidload of choiceVC中检查变量

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

实现UITableView的didSelect委托方法

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

备注:

希望这段代码对您有用。这对我来说很糟糕。

要在其中打开使用文本字段委托的文本文件表单的第一个视图控制器tableview.for。

第一个视图控制器

 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);
    }
    

第二个视图控制器对于 tableview 显示并将数据从第二个控制器传递到第一个控制器

第二视图控制器

var delegate: Delegate?

var tblData: [String] = [String]()

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)
}

如果您正在寻找 select 选项的选择器视图替代方案,您可以使用像控件这样的下拉菜单,例如

  1. DropDown
  2. RSSelectionMenu

希望这些库能解决你的问题,祝你好运