ios - 在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 的索引路径。是否可以。提前致谢!
解决方案
试试这段代码,复制粘贴
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)
}
推荐阅读
- php - 如何将关联数组传递给PHP中的函数参数
- spring-security - 如何打印 Spring Webflux 应用程序的过滤器链顺序列表?
- reporting-services - ssrs-在数据区域创建多行,如果没有数据则不显示数据消息
- mongodb - 为什么 Postman 中的 HTTP 响应正文显示的字段比 Chrome 开发工具多?
- linux - 如何从 Windows 主机访问我的设备到 docker linux 容器?
- javascript - 获取、累加和写入所有值到某个类的元素
- swagger - AWS Cloudformation - 无法从嵌套堆栈中引用 Swagger 中的资源
- php - 为什么在 php.in 中编辑 upload_max_filesize 不起作用?
- javascript - 识别并触发“下拉/选择器”上的事件
- java - 有没有办法在解组到 Java 对象时替换无效的 XML 数据?