我有一个表格视图,它在单元格中有一个文本字段(以编程方式添加)。我使用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!
}
发布于 2018-10-25 03:56:11
跟踪所需的文本,在cellForRowAt中只需将其添加到文本字段即可。
https://stackoverflow.com/questions/52973608
复制相似问题