【问题标题】:How to enable UITableView when there is text in a textfield当文本字段中有文本时如何启用 UITableView
【发布时间】:2019-10-25 12:33:25
【问题描述】:

我有两个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 创建了一个扩展,它基本上“启用”和“禁用”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 显示为“禁用”,但是当我开始输入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

【问题讨论】:

  • 能分享一下演示应用吗?

标签: ios swift uitableview text uitextfield


【解决方案1】:

tableView.reloadData() 由于没有重新加载表视图而丢失。

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

【讨论】:

    【解决方案2】:

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

    在您的 ViewController 类中:

            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
       }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多