iosswiftuitableviewiqkeyboardmanager

How to get Indexpath of textfield which placed in tableview when tap on done button using keyboard IQKeyboardManagerSwift in swift


I am new in swift and I am using IQKeyboardManagerSwift library for textfield and textview. I am not able to send Indexpath from tableview to selector function. My code is like this

TableViewCell

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "TimeSheetTableViewCell", for: indexPath) as? TimeSheetTableViewCell else {
            return UITableViewCell()
        }
        cell.txtTimeSheet.text = arrcheck[indexPath.row]
        cell.txtTimeSheet.keyboardToolbar.doneBarButton.setTarget(self, action: #selector(doneButtonClicked))
        return cell
    }

 @objc func doneButtonClicked(_ sender: Any) {
        print("Done")
        
    }

I want Indexpath of tableview in donebuttonClick function. Is it possible. Thanks in Advance!


Solution

  • try this code, just copy and paste

     set UITextFieldDelegate, UITextViewDelegate 
           
         var strViewFooter = ""
         var textFieldIndexPath = IndexPath() 
         
       func numberOfSections(in tableView: UITableView) -> Int {
            arraycheck.count
        }
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            1
        }
       func textViewDidEndEditing(_ textView: UITextView) {
            strViewFooter = textView.text
        }
        func textFieldDidEndEditing(_ textField: UITextField) {
                        
             let cell: UITableViewCell = textField.superview?.superview as! UITableViewCell
              let table: UITableView = cell.superview as! UITableView
                 textFieldIndexPath = table.indexPath(for: cell)!
              }
                        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
             guard let cell = tableView.dequeueReusableCell(withIdentifier: "TimeSheetTableViewCell", for: indexPath) as? TimeSheetTableViewCell else {
                  return UITableViewCell()
                }
               cell.txtTimeSheet.keyboardToolbar.doneBarButton.setTarget(self, action: #selector(doneButtonClicked))
                return cell
            }
          
        func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
                let vw = UIView()
                vw.backgroundColor = UIColor.clear
                let titleLabel = UITextView(frame: CGRect(x:10,y: 5 ,width:350,height:150))
                titleLabel.backgroundColor = UIColor.clear
                titleLabel.font = UIFont(name: "Montserrat-Regular", size: 12)
                titleLabel.text  = arrayFootter[section]
                titleLabel.tag = section
                vw.addSubview(titleLabel)
                titleLabel.keyboardToolbar.doneBarButton.setTarget(self, action: #selector(donebuttonClickedOnView))
                return vw
            }
        
             func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
                return 50
            }
                  
               @objc func doneButtonClicked(_ sender: Any) {
                     print(textFieldIndexPath)
                     print(textFieldIndexPath.row)
                     print(textFieldIndexPath.section)
                    
                  }
           @objc func donebuttonClickedOnView(_ sender: UIBarButtonItem){
                arrayFootter.remove(at: sender.tag)
               arrayFootter.insert(strViewFooter, at: sender.tag)
               print(sender.tag)
            }