簡體   English   中英

當文本字段中有文本時如何啟用UITableView

[英]How to enable UITableView when there is text in a textfield

我有兩個textFields和一個TableView 最初應禁用Tableview (這意味着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 (從中檢索線程):

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

最后,我在isUserEditing中實現了此方法以及變量viewForHeaderInSection

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並自動更新嗎? 任何幫助表示贊賞,謝謝!

當textField中沒有文本時,這應該是初始視圖

兩個textFields中都有一些文本后,應該是該視圖

由於未重新加載該表視圖,因此缺少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
   }

如果要在出現鍵盤時啟用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
   }

希望這能解決您的問題,祝您好運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM