Advertisement
Don_Mag

Untitled

Apr 5th, 2023
988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.67 KB | None | 0 0
  1.  
  2. struct TwoTFData {
  3.     var cStr: String = ""
  4.     var dStr: String = ""
  5. }
  6. class AuthTVC: UITableViewCell {
  7.     let tfC = UITextField()
  8.     let tfD = UITextField()
  9.    
  10.     override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  11.         super.init(style: style, reuseIdentifier: reuseIdentifier)
  12.         commonInit()
  13.     }
  14.     required init?(coder: NSCoder) {
  15.         super.init(coder: coder)
  16.         commonInit()
  17.     }
  18.     func commonInit() {
  19.  
  20.         [tfC, tfD].forEach { v in
  21.             v.borderStyle = .roundedRect
  22.             v.backgroundColor = .yellow
  23.             v.translatesAutoresizingMaskIntoConstraints = false
  24.             contentView.addSubview(v)
  25.         }
  26.         let g = contentView.layoutMarginsGuide
  27.         NSLayoutConstraint.activate([
  28.             tfC.topAnchor.constraint(equalTo: g.topAnchor, constant: 0.0),
  29.             tfC.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 0.0),
  30.             tfC.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: 0.0),
  31.  
  32.             tfD.topAnchor.constraint(equalTo: g.topAnchor, constant: 0.0),
  33.             tfD.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: 0.0),
  34.             tfD.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: 0.0),
  35.            
  36.             tfD.leadingAnchor.constraint(equalTo: tfC.trailingAnchor, constant: 20.0),
  37.             tfD.widthAnchor.constraint(equalTo: tfC.widthAnchor),
  38.         ])
  39.     }
  40. }
  41. class TFCrashVC: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {
  42.    
  43.     let txtfldLoc = UITextField()
  44.     let txtfldOp = UITextField()
  45.  
  46.     let authTableView = UITableView()
  47.    
  48.     var myData: [TwoTFData] = []
  49.    
  50.     override func viewDidLoad() {
  51.         super.viewDidLoad()
  52.        
  53.         [txtfldLoc, txtfldOp].forEach { v in
  54.             v.borderStyle = .roundedRect
  55.             v.delegate = self
  56.             v.translatesAutoresizingMaskIntoConstraints = false
  57.             view.addSubview(v)
  58.         }
  59.         txtfldLoc.placeholder = "Location"
  60.         txtfldOp.placeholder = "Operator"
  61.         txtfldLoc.backgroundColor = .cyan
  62.         txtfldOp.backgroundColor = .green
  63.        
  64.         authTableView.translatesAutoresizingMaskIntoConstraints = false
  65.         view.addSubview(authTableView)
  66.        
  67.         let g = view.safeAreaLayoutGuide
  68.         NSLayoutConstraint.activate([
  69.            
  70.             txtfldLoc.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
  71.             txtfldLoc.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
  72.            
  73.             txtfldOp.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
  74.             txtfldOp.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
  75.            
  76.             txtfldOp.leadingAnchor.constraint(equalTo: txtfldLoc.trailingAnchor, constant: 20.0),
  77.             txtfldOp.widthAnchor.constraint(equalTo: txtfldLoc.widthAnchor),
  78.            
  79.             authTableView.topAnchor.constraint(equalTo: txtfldLoc.bottomAnchor, constant: 20.0),
  80.             authTableView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
  81.             authTableView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
  82.             authTableView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),
  83.  
  84.         ])
  85.    
  86.         authTableView.register(AuthTVC.self, forCellReuseIdentifier: "c")
  87.         authTableView.dataSource = self
  88.         authTableView.delegate = self
  89.        
  90.         // start with one data element
  91.         let d: TwoTFData = .init(cStr: "Location 0", dStr: "Operator 0")
  92.         myData.append(d)
  93.  
  94.     }
  95.    
  96.     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  97.         return myData.count
  98.     }
  99.     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  100.         let c = tableView.dequeueReusableCell(withIdentifier: "c", for: indexPath) as! AuthTVC
  101.         c.tfC.text = myData[indexPath.row].cStr
  102.         c.tfD.text = myData[indexPath.row].dStr
  103.         return c
  104.     }
  105.    
  106.     func locationButtonTapped(_ tf: UITextField) {
  107.         let d: TwoTFData = .init(cStr: "Location \(myData.count)", dStr: "")
  108.         myData.append(d)
  109.         authTableView.reloadData()
  110.     }
  111.     func operatorButtonTapped(_ tf: UITextField) {
  112.         let d: TwoTFData = .init(cStr: "", dStr: "Operator \(myData.count)")
  113.         myData.append(d)
  114.         authTableView.reloadData()
  115.     }
  116.  
  117.     func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
  118.        
  119. //      textField.layoutIfNeeded()
  120. //      textField.spellCheckingType = .no
  121. //      textField.autocorrectionType = .no
  122.        
  123.         if textField == txtfldLoc {
  124.             self.locationButtonTapped(textField)
  125.             return false
  126.         }
  127.         else if textField == txtfldOp {
  128.             self.operatorButtonTapped(textField)
  129.             return false
  130.         }
  131.         else if textField.superview?.isDescendant(of: self.authTableView) == true
  132.         {
  133. //          if textField.accessibilityLabel != nil{
  134. //
  135. //              let index = Int(textField.accessibilityLabel!)!
  136. //
  137. //              let cell = self.authTableView.cellForRow(at: IndexPath(row: index, section: 0)) as! AuthTVC
  138. //              self.selectedRow = IndexPath(row: index, section: 0)
  139. //              if authenticatorObjectList.count > index  {
  140. //                  let authenticator = self.authenticatorObjectList[index]
  141. //
  142. //                  cell.authenticatorTextField.defaultPlaceholderColor = UIColor(hex: Color.shared.blue)!
  143. //                  cell.authenticatorTextField.placeholderColor = UIColor(hex: Color.shared.blue)!
  144. //                  cell.authenticatorTextField.textColor = .black
  145. //
  146. //                  if authenticator.dataType == "List" {
  147. //                      self.authenticatorButtonTapped(sender:textField)
  148. //                      return false
  149. //                  }
  150. //                  else {
  151. //                      if authenticator.regex.first == "^"  {
  152. //
  153. //                          if let txtfld  = self.arrOfViews.first(where:{$0.accessibilityLabel == "\(index)"}) as? MFTextField
  154. //
  155. //                          {
  156. //
  157. //                              if let ind = self.arrOfViews.firstIndex(of: txtfld)
  158. //                              {
  159. //                                  cell.authenticatorTextField.text = txtfld.text
  160. //                              }
  161. //                          }
  162. //                          DispatchQueue.main.async {
  163. //                              self.authTableView.layoutIfNeeded()
  164. //                          }
  165. //                          return true
  166. //                      }
  167. //                  }
  168. //              }
  169. //          }
  170. //
  171. //      } else if textField == amountTextField {
  172.            
  173.            
  174.         }
  175.         return true
  176.     }
  177. }
  178.  
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement