ios - tableview.reload()后TextField数据消失
问题描述
我有一个表格视图,它在单元格中有一个文本字段(以编程方式添加)。我正在使用 editActionsForRowAt 方法插入和删除单元格,但是每次插入/删除时,我之前输入的文本字段数据都会消失。我认为是因为 tableview.reload 方法。你能请教吗?
非常感谢!
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let myAction = UITableViewRowAction(style: .normal, title: "INSERT") { (action, indexPath) in
print("I'm here")
self.data[indexPath.section].subType.insert("Set", at: indexPath.item)
// self.data[indexPath.section].subType.insert("Set", at: indexPath.section)
print("inserttt")
self.tblView.reloadData();
}
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
// if editingStyle == .delete{
print("delete")
self.data[indexPath.section].subType.remove(at: indexPath.row)
self.tblView.reloadData()
// }
}
// tableView.keyboardDismissMode = .onDrag
return [myAction,delete]
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cells")
// cell?.textLabel?.text = data[indexPath.section].subType[indexPath.row]
cell?.textLabel?.text = data[indexPath.section].headerName
var sampleTextField = UITextField(frame: CGRect(x: 180, y: 10, width: 60, height: 40))
sampleTextField.borderStyle = UITextBorderStyle.roundedRect
sampleTextField.backgroundColor = #colorLiteral(red: 0.9027962089, green: 0.781386435, blue: 0.9435198903, alpha: 1)
sampleTextField.tag=indexPath.item
//use this if you want to clear test when you edit again
// sampleTextField.clearsOnBeginEditing = true
sampleTextField.placeholder=data[indexPath.section].headerName
headerSection=data[indexPath.section].headerName!
cell?.addSubview(sampleTextField)
sampleTextField.addTarget(self, action: #selector(UITextFieldDelegate.textFieldDidEndEditing(_:)), for: UIControlEvents.editingDidEnd)
return cell!
}
解决方案
跟踪您想要的文本,然后在 cellForRowAt 中将其添加到文本字段中。
推荐阅读
- c - C中没有参数的函数
- bigcommerce - 如何从本地开发环境访问 Bigcommerce Stencil 主题编辑器?
- c# - 在 .Net Core 中,如何在 HttpContext 的控制器中设置变量以在所有控制器方法中使用?
- google-api - Google Drive API v3 & Google Picker:更改文件的下载权限
- swift - 如何自定义 MKAnnotation pin 的图像
- azure - 在 Azure DW(突触)中使用 Max 进行数据透视
- python - 调用函数来检查输入是否能被 7 整除
- json - 如何使用 Flutter 将 Json 数据中的所有图像显示到 Carousal 中
- arrays - 如何根据相同的键将一个对象放入另一个对象中?
- python-3.x - 使用 Voila 运行 ipynb 文件时,Chrome 浏览器中不会显示任何交互式小部件