简体   繁体   English

UITableView上的reloadData后未选择文本字段

[英]Textfields don't get selected after reloadData on UITableView

In my UITableView I have rows which contain 4 textfields each. 在我的UITableView中,我有各包含4个文本字段的行。 After user ends editing couple of things in UITableView datasource are being recalculated, so I need to reload UITableView after edition of textfield. 用户结束编辑后,将重新计算UITableView数据源中的几件事,因此我需要在文本字段版本之后重新加载UITableView。 But because tableview is being reloaded, when user touches another textfield to edit while editing the first one, second textfield doesn't get selected. 但是因为正在重新加载tableview,所以当用户在编辑第一个文本框时触摸另一个文本框进行编辑时,不会选择第二个文本框。 He has to touch it again before it can be edited. 他必须再次触摸它才能对其进行编辑。

Is there a way to avoid this behaviour? 有办法避免这种行为吗? Unfortunately I do need to use reloadData instead of reloadSections or reloadRows methods. 不幸的是,我确实需要使用reloadData而不是reloadSections或reloadRows方法。 Any help will be appreciated, thanks! 任何帮助将不胜感激,谢谢!

Reloading a table view will resign the first responder of the text field or even text view. 重新加载表格视图将使文本字段甚至文本视图的第一响应者辞职。 You could try this trick if you have multiple changes that need to be made. 如果您需要进行多项更改,则可以尝试使用此技巧。

[[self tableView] beginUpdates]
[[self tableView] endUpdates]

Try assigning a gestureRecognizer to each field or same one to all fields. 尝试为每个字段分配一个gestureRecognizer或为所有字段分配一个相同的gestureRecognizer Once you capture the touch in required field, capture the tag and make that field the firstResponder . 在必填字段中捕获touch后,请捕获tag并将该字段设置为firstResponder

Summing up all the help I received and my own ideas I've gone with the following: 总结一下我得到的所有帮助和我自己的想法,我做了以下工作:

-Delay reload for 0.01 sec so textFieldDidBeginEditing gets called -延迟重新加载0.01秒,因此调用textFieldDidBeginEditing

-Then save textfield's tag and cell's index path to variables in textFieldDidBeginEditing -然后将文本字段的标记和单元格的索引路径保存到textFieldDidBeginEditing变量

-Call reloadData on tableview -在tableview上调用reloadData

-Call becomeFirstResponder on textfield saved with variables -在使用变量保存的文本becomeFirstResponder上调用成为becomeFirstResponder

This however caused keyboard hiding for a brief moment as it didn't have any responder during reloading. 但是,这导致键盘隐藏了片刻,因为重新加载期间没有任何响应程序。 To resolve that I created a fake textfield in tableview's superview and assigned him as a responder up to a moment when tableview was reloaded to assign back to the textfield that was selected by a user. 为了解决这个问题,我在tableview的超级视图中创建了一个伪造的文本字段,并在重新加载tableview时将他分配为响应者,以分配回用户选择的文本字段。

Certainly not the cleanest solution, but it works. 当然,这不是最干净的解决方案,但是它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM