0

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!

Jins George
  • 121
  • 6
Muju
  • 884
  • 20
  • 54

3 Answers3

2

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)
        }
Jins George
  • 121
  • 6
  • Perfect your code work just I add cell.txtTimeSheet.delegate = self in cellforrow method. Thank You – Muju Jul 30 '20 at 10:08
  • @objc func doneButtonClicked(_ sender: Any) { print(arrcheck[textFieldIndexPath.row]) } – Jins George Jul 30 '20 at 11:18
  • But I replace textfiled data But it is only printing old data. I need Current textfiled.text in doneButtonClicked – Muju Jul 30 '20 at 11:22
  • 1
    when you replace the data in textfield, at the same time you must replace the data in your array(arrcheck). like this. func textFieldDidEndEditing(_ textField: UITextField) { let cell: UITableViewCell = textField.superview?.superview as! UITableViewCell let table: UITableView = cell.superview as! UITableView textFieldIndexPath = table.indexPath(for: cell)! arraycheck.remove(at: textFieldIndexPath.row) arraycheck.insert(textField.text!, at: textFieldIndexPath.row) } – Jins George Jul 30 '20 at 11:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218889/discussion-between-jins-george-and-muju). – Jins George Jul 30 '20 at 11:34
  • I added textview in viewForFooterInSection how can I do the same thing. Can you please help! – Muju Aug 16 '20 at 15:02
0

first make a variable like this in your app that you can use it in cellForRowAt, like this:

var appIndexPathRow = Int()

then put this code in your cellForRowAt:

appIndexPathRow  = indexPath.row

Those code would work if you got only 1 section in your tableview, if you have more than 1 I should give you another code! after all you can print your index in button action! have fun

PS: I just gave you what you put in your question, but i would use it in didSelectRowAt instead of cellForRowAt if it was my project!

  • I need indexpath on doneButtonClicked method as I am using textfield and I need to add textfield data in array. – Muju Jul 30 '20 at 09:29
0

Get indexpath on click Button.

@objc func doneButtonClicked(_ sender: Any) {
let tag = sender.tag
let index = IndexPath(row: tag, section: 0)
let cell = tableView.cellForRow(at: index) as! tableView
}