ios - 在重新加载 TableView 或重新加载特定行或插入和删除时隐藏键盘

标签 ios keyboard

我有一个 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 放入您的 View controller(Storyboard)
  • 随心所欲地赋予约束(无所谓)
  • 始终在 Storyboard中保留隐藏的虚拟 UITextField
  • 将隐藏的虚拟 UITextFieldoutlet 放入类文件中。
  • 现在是时候在 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
        }
    }

Screenshot of Storyboard

请参阅随附的演示应用程序源代码。如果需要任何进一步的帮助。

Source Code

关于ios - 在重新加载 TableView 或重新加载特定行或插入和删除时隐藏键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55535068/

相关文章:

ios - 来自外部脚踏开关的 Cordova 应用程序按键事件在 iOS 上不起作用

ios - 如何通过闭包将数据从一个 ViewController 传输到另一个

iphone - 如何从 plist 加载值?

iOS 应用程序 Modbus 使用 Swift 读/写

android - Apache Cordova InAppBrowser 显示键盘并立即隐藏它

c# - 当已经按下第二个键时如何检测键向下/向上

ios - JPG 图像不加载 UIImage imageNamed

ios - 内在内容大小在UITableView内部的UICollectionView

linux - 如何使用输入子系统在 Linux 上使用键盘事件

keyboard - 我可以自定义 LinqPad 快捷键吗?