Don_Mag icon

Untitled

Don_Mag | PRO | 04/05/23 12:54:05 PM UTC | 0 ⭐ | 7934 👁️ | Never ⏰ | []
Swift |

5.67 KB

|

None

|

0 👍

/

0 👎

 
struct TwoTFData {
    var cStr: String = ""
    var dStr: String = ""
}
class AuthTVC: UITableViewCell {
    let tfC = UITextField()
    let tfD = UITextField()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        commonInit()
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }
    func commonInit() {
 
        [tfC, tfD].forEach { v in
            v.borderStyle = .roundedRect
            v.backgroundColor = .yellow
            v.translatesAutoresizingMaskIntoConstraints = false
            contentView.addSubview(v)
        }
        let g = contentView.layoutMarginsGuide
        NSLayoutConstraint.activate([
            tfC.topAnchor.constraint(equalTo: g.topAnchor, constant: 0.0),
            tfC.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 0.0),
            tfC.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: 0.0),
 
            tfD.topAnchor.constraint(equalTo: g.topAnchor, constant: 0.0),
            tfD.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: 0.0),
            tfD.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: 0.0),
            
            tfD.leadingAnchor.constraint(equalTo: tfC.trailingAnchor, constant: 20.0),
            tfD.widthAnchor.constraint(equalTo: tfC.widthAnchor),
        ])
    }
}
class TFCrashVC: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {
    
    let txtfldLoc = UITextField()
    let txtfldOp = UITextField()
 
    let authTableView = UITableView()
    
    var myData: [TwoTFData] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        [txtfldLoc, txtfldOp].forEach { v in
            v.borderStyle = .roundedRect
            v.delegate = self
            v.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(v)
        }
        txtfldLoc.placeholder = "Location"
        txtfldOp.placeholder = "Operator"
        txtfldLoc.backgroundColor = .cyan
        txtfldOp.backgroundColor = .green
        
        authTableView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(authTableView)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            
            txtfldLoc.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
            txtfldLoc.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
            
            txtfldOp.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
            txtfldOp.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
            
            txtfldOp.leadingAnchor.constraint(equalTo: txtfldLoc.trailingAnchor, constant: 20.0),
            txtfldOp.widthAnchor.constraint(equalTo: txtfldLoc.widthAnchor),
            
            authTableView.topAnchor.constraint(equalTo: txtfldLoc.bottomAnchor, constant: 20.0),
            authTableView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
            authTableView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
            authTableView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),
 
        ])
    
        authTableView.register(AuthTVC.self, forCellReuseIdentifier: "c")
        authTableView.dataSource = self
        authTableView.delegate = self
        
        // start with one data element
        let d: TwoTFData = .init(cStr: "Location 0", dStr: "Operator 0")
        myData.append(d)
 
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return myData.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let c = tableView.dequeueReusableCell(withIdentifier: "c", for: indexPath) as! AuthTVC
        c.tfC.text = myData[indexPath.row].cStr
        c.tfD.text = myData[indexPath.row].dStr
        return c
    }
    
    func locationButtonTapped(_ tf: UITextField) {
        let d: TwoTFData = .init(cStr: "Location \(myData.count)", dStr: "")
        myData.append(d)
        authTableView.reloadData()
    }
    func operatorButtonTapped(_ tf: UITextField) {
        let d: TwoTFData = .init(cStr: "", dStr: "Operator \(myData.count)")
        myData.append(d)
        authTableView.reloadData()
    }
 
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        
//      textField.layoutIfNeeded()
//      textField.spellCheckingType = .no
//      textField.autocorrectionType = .no
        
        if textField == txtfldLoc {
            self.locationButtonTapped(textField)
            return false
        }
        else if textField == txtfldOp {
            self.operatorButtonTapped(textField)
            return false
        }
        else if textField.superview?.isDescendant(of: self.authTableView) == true
        {
//          if textField.accessibilityLabel != nil{
//
//              let index = Int(textField.accessibilityLabel!)!
//
//              let cell = self.authTableView.cellForRow(at: IndexPath(row: index, section: 0)) as! AuthTVC
//              self.selectedRow = IndexPath(row: index, section: 0)
//              if authenticatorObjectList.count > index  {
//                  let authenticator = self.authenticatorObjectList[index]
//
//                  cell.authenticatorTextField.defaultPlaceholderColor = UIColor(hex: Color.shared.blue)!
//                  cell.authenticatorTextField.placeholderColor = UIColor(hex: Color.shared.blue)!
//                  cell.authenticatorTextField.textColor = .black
//
//                  if authenticator.dataType == "List" {
//                      self.authenticatorButtonTapped(sender:textField)
//                      return false
//                  }
//                  else {
//                      if authenticator.regex.first == "^"  {
//
//                          if let txtfld  = self.arrOfViews.first(where:{$0.accessibilityLabel == "\(index)"}) as? MFTextField
//
//                          {
//
//                              if let ind = self.arrOfViews.firstIndex(of: txtfld)
//                              {
//                                  cell.authenticatorTextField.text = txtfld.text
//                              }
//                          }
//                          DispatchQueue.main.async {
//                              self.authTableView.layoutIfNeeded()
//                          }
//                          return true
//                      }
//                  }
//              }
//          }
//
//      } else if textField == amountTextField {
            
            
        }
        return true
    }
}
 
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎