簡體   English   中英

如何使用一個不是tableview成員的開關禁用tableview單元的所有文本字段?

[英]How can I disable all textfield of my tableview cell with one switch which not member of tableview?

我有一個帶有textField的自定義tableViewCell ,只有一個開關不是tableViewCell的一部分。

所以我想在打開和關閉然后關閉和啟用tableViewCell textFields 那我該怎么辦呢?

@IBAction func switchServiceTax_Action(_ sender: UISwitch) {


    let cell : tbleCell = tblViewServices.dequeueReusableCell(withIdentifier: "cell") as! tbleCell


   if switch.isOn{
        cell.txtFld.isUserInteractionEnabled = true
        tblView.reloadData()

   }else{
        cell.txtFld.isUserInteractionEnabled = false
    tblView.reloadData()


   }
}

有兩種方法可以達到要求。

  1. cellforRow ,您可以直接在下面的行中使用if-else而不是if-else ,並確保每次切換開關時都重新加載tableview。

cell.txtFld.isUserInteractionEnabled = switch.isOn

  1. 只需在UITextFieldDelgate下面實現即可,僅此而已 無需重新加載tableview,因此可以提高應用程序性能。

     func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { return switch.isOn } 

您可以在單元格中為行使用開關對象。 這是簡單的邏輯,請在下面編寫並遵循說明。

@IBAction weak var switchTextField: UISwitch!
@IBAction func switchServiceTax_Action(_ sender: UISwitch) {
    tblView.reloadData()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TBLcell
    cell.txtFirstName.resignFirstResponder()
    cell.txtFirstName.isUserInteractionEnabled = switchTextField.isOn
    return cell
}

暫無
暫無

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

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