当文本字段中有文本时如何启用 UITableView

How to enable UITableView when there is text in a textfield

我有两个 textFields 和一个 TableViewTableview 最初应该被禁用(意味着 labels 变灰并且 cells 不可点击)。当 textFields 都有值时,我希望 TableViewtableViewCells 可以点击并且不会变灰。 到目前为止,我已将目标添加到 textfields 以跟踪用户何时编辑:

var isUserEditing: Bool = false

func setup() {
        mealItemTextField.addTarget(self, action: #selector(userIsEditing), for: UIControl.Event.editingChanged)
        priceTextField.addTarget(self, action: #selector(userIsEditing), for: UIControl.Event.editingChanged)
    }

@objc func userIsEditing(sender: UITextField) {
        sender.text = sender.text?.trimmingCharacters(in: .whitespaces)
        guard
            let mealtext = mealItemTextField.text, !mealtext.isEmpty,
            let pricetext = priceTextField.text, !pricetext.isEmpty
        else
        {
            isUserEditing = false
            return
        }
        isUserEditing = true
    }

然后我为 UITableViewCell 创建了一个扩展,基本上 "enables" 和 "disables" tableView(从 this 线程中检索):

  extension UITableViewCell {
    func enable(on: Bool) {
        for view in contentView.subviews {
            view.isUserInteractionEnabled = on
            view.alpha = on ? 1 : 0.5
        }
    }
}

最后,我在 viewForHeaderInSection:

中实现了这个方法以及变量 isUserEditing
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let userModel = Data.userModels[section]
    let cell = tableView.dequeueReusableCell(withIdentifier: "nameCell") as! NameHeaderTableViewCell
    cell.setup(model: userModel)
    cell.enable(on: false)
    if isUserEditing == true {
        cell.enable(on: true)
    }
    return cell.contentView
}

视图加载时 tableView 出现 "disabled",但是当我开始输入一个或两个 textFields 时,它仍然处于禁用状态。所以很明显 viewForHeaderInSection 没有重新加载,也没有被再次执行。

有什么办法可以实现吗?我可以实时 运行 viewForHeaderInSection 并自动更新吗?感谢任何帮助,谢谢!

This should be the initial view when there is no text in the textField

This should be the view after there is some text in both textFields

tableView.reloadData() 丢失,导致 table 视图未重新加载。

@objc func userIsEditing(sender: UITextField) {
       sender.text = sender.text?.trimmingCharacters(in: .whitespaces)
       guard
           let mealtext = mealItemTextField.text, !mealtext.isEmpty,
           let pricetext = priceTextField.text, !pricetext.isEmpty
       else
       {
           isUserEditing = false
       tableView.reloadData() // reload table view here
           return
       }
       isUserEditing = true
    tableView.reloadData() // reload table view here
   }

如果您想在出现键盘和文本字段是否为空时启用您的 tableView,您可以通过通知进行检查;

在你的 ViewController class 中:

        override func viewDidLoad() {
             super.viewDidLoad()
             yourTableView.isUserInteractionEnabled = false

         // check notifications if keyboard shown or not
            NotificationCenter.default.addObserver(self, selector: #selector(showKeyboard), name: UIResponder.keyboardWillShowNotification, object: nil)     
            NotificationCenter.default.addObserver(self, selector: #selector(hideKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)

         // if your textFields out of your tableview
              yourTextField1.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
                yourTextField2.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)

                }


 @objc func textFieldDidChange(textField: UITextField){

     self.yourTableView.isUserInteractionEnabled = true
     print("Text changed")

  }

  // show keyboard function
  @objc func showKeyboard(notification: NSNotification){

  // You can disable your tableview or your cells in this case i just disable tableview.

     yourTableView.isUserInteractionEnabled = true
     print("keyboard appeared")
   }

   // this part is up to how you want to implement. If you want to keep tableview active after textField edited don't use this method

  @objc func hideKeyboard(notification: NSNotification){

   // You can disable your tableview or your cells
      yourTableView.isUserInteractionEnabled = false
      print("keyboard disappeared")
   }

   func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
   let userModel = Data.userModels[section]
   let cell = tableView.dequeueReusableCell(withIdentifier: "nameCell") as! NameHeaderTableViewCell

        // if your text Fields inside tableView tracking you textfield changes (in this case you couldn't disable tableView initially) 

   cell.yourTextField1.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
   cell.yourTextField2.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
                return cell.contentView
   }

希望这能解决您的问题,祝您好运。