首页 > 解决方案 > 在swift中使用键盘IQKeyboardManagerSwift点击完成按钮时如何获取放置在tableview中的文本字段的索引路径

问题描述

我是 swift 的新手,我正在使用 IQKeyboardManagerSwift 库来处理文本字段和文本视图。我无法将 Indexpath 从 tableview 发送到选择器函数。我的代码是这样的

表视图单元格

 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")
        
    }

我想要在 donebuttonClick 函数中的 tableview 的索引路径。是否可以。提前致谢!

标签: iosswiftuitableviewiqkeyboardmanager

解决方案


试试这段代码,复制粘贴

 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)
        }

推荐阅读