我有一个 tableview 单元格,其中有另一个 tableview,其中我有文本字段,如果我更改文本字段的值,我需要重新加载 TableView 或重新加载该特定行,但它会在重新加载表时隐藏键盘。有什么办法让键盘保持原样吗???
我尝试遵循 stackoverflow 的建议,但它对我不起作用。
Keep UIKeyboard up while reloading UITableView section
也试试下面的代码。
[self.tblADMEDesign beginUpdates];
[self.tblADMEDesign insertRowsAtIndexPaths:@[selectedIndexPath] withRowAnimation:UITableViewRowAnimationRight];
[self.tblADMEDesign deleteRowsAtIndexPaths:@[selectedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tblADMEDesign endUpdates];
[self.tblADMEDesign beginUpdates];
[self.tblADMEDesign endUpdates];
[self.tblADMEDesign beginUpdates];
[self.tblADMEDesign reloadRowsAtIndexPaths:@[selectedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tblADMEDesign endUpdates];
最佳答案
This是在重新加载 UITableView
内容时保持键盘打开的最佳解决方案。
如果它不适合您,那么快速破解总是有用的。
请按照以下步骤操作。
- 将一个虚拟
UITextField
放入您的 Viewcontroller(Storyboard)
- 随心所欲地赋予
约束
(无所谓) - 始终在 Storyboard中保留隐藏的虚拟
UITextField
。 - 将隐藏的虚拟
UITextField
的outlet
放入类文件中。 - 现在是时候在
reloadData()
之前进行黑客攻击了,只需调用txtDummy.becomeFirstResponder()
引用下面我为已解决的问题所做的代码。
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var txtDummy: UITextField!
@IBOutlet weak var tblView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tblView.register(UINib.init(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "CustomTableViewCell")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
cell.txtField?.text = "text"
return cell
}
@IBAction func reloadtable(){
txtDummy.becomeFirstResponder()
tblView.reloadData()
//Perform other operation after some delay
}
}
请参阅随附的演示应用程序源代码。如果需要任何进一步的帮助。
关于ios - 在重新加载 TableView 或重新加载特定行或插入和删除时隐藏键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55535068/