编辑完成后,如何防止表格单元格的文本视图可编辑?
所以这是一个有趣的问题。我有包含文本字段的自定义表格视图单元格。当在我的 cellForRowAtIndexPath 中时,我有一个 if 语句来确定单元格的文本字段是否应该可编辑 - 它看起来像这样:
(self.isEditing) ? [infoCell.textField setEnabled:YES] : [infoCell.textField setEnabled:NO];
这实际上工作得很好 - 除了我遇到的问题。它使得当显示表视图时,无法编辑行的文本字段。当用户单击“编辑”将其置于编辑模式时,文本字段就可以进行编辑。
问题:当我编辑字段并单击“完成”时,它会返回到常规表格视图,但键盘保持可见,并且我正在编辑的最后一个单元格的文本字段仍然可编辑。
应该发生什么:键盘应该消失,所有单元格的文本字段应该不再可编辑。
关于可能出什么问题的任何想法吗?需要寻找什么?
谢谢!
So this is an interesting problem. I have custom tableviewcells that include a text field. When In my cellForRowAtIndexPath I have an if statement that determines whether or not the cell's text field should be editable- it looks like this:
(self.isEditing) ? [infoCell.textField setEnabled:YES] : [infoCell.textField setEnabled:NO];
This actually works well - except for the issue I'm having. It makes it so that when the tableview is displayed, the rows' text field cannot be edited. When the user clicks "Edit" to put it into editing mode, then the text fields are enabled for editing.
The Problem: When I am editing a field, and click "Done", it goes back to the regular tableview but the keyboard stays visible and the last cell's text field I was editing continues to be editable.
What Should happen: The keyboard should go away and all the cells' text fields should no longer be editable.
Any ideas about what could be going wrong? Things to look for?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,禁用 UITextField 不会关闭键盘。您需要保留指向当前 UITextField 的指针。首先,在头文件中创建一个实例变量:
然后,实现 UITextFieldDelegate 协议。肮脏的工作将通过以下方法完成:
最后,当您准备好关闭键盘并禁用文本字段时,只需调用:
祝您好运!
Unfortunately, disabling the UITextField won't dismiss the keyboard. You'll need to retain a pointer to your current UITextField. First, create an instance variable in your header file:
Then, implement the UITextFieldDelegate protocol. The dirty work will be done in the following method:
Finally, when you're ready to dismiss the keyboard and disable your textFields, simply call:
Good luck!
我发现 self.isEditing 不可靠。如果您正在编辑单个单元格,则其工作方式与处于“编辑模式”时不同。
为了解决这个问题,我所做的就是,每当我想对所有其他单元格执行某些操作时,我只需迭代表视图的visibleCells 方法并手动调整它们。您必须考虑当新单元格变得可见时会发生什么,但这取决于您的实现。
PS - 显然,在遍历可见方块时,您可能想跳过有问题的单元格。取决于你在做什么。
I've found self.isEditing to be unreliable. If you are editing an individual cell, it works differently from when you are in "edit mode".
What I've done to get around it is, whenever I want to do something to all other cells, I just iterate through my table view's visibleCells method and manually adjust them. You'll have to consider what happens when new cells become visible, but that's up to your implementation.
PS - obviously you may want to skip the cell in question when iterating through the visible squares. depends on what you're doing.