我有一个表格视图,该表格视图的单元格中有一个文本字段(以编程方式添加)。我正在使用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!
}
答案 0 :(得分:0)
跟踪所需的文本,并在cellForRowAt中将其添加到文本字段中。