简体   繁体   English

尝试在编辑时使带有UITextField的自定义UITableViewCell成为FirstResponder

[英]Trying to make a Custom UITableViewCell with a UITextField becomeFirstResponder when Editing

I have a UITableViewController with custom UITableViewCell s that contain a UITextField . 我有一个UITableViewController使用自定义UITableViewCell包含A S UITextField When switching the table view into edit mode, I add a new cell to the bottom of the table and would like to make this cell becomeFirstResponder . 将表格视图切换到编辑模式时,我在表格底部添加了一个新单元格,并希望使该单元格becomeFirstResponder My tableView:cellForRowAtIndexPath: method checks for this bottom cell, so I just added the line: 我的tableView:cellForRowAtIndexPath:方法检查此底部单元格,因此我仅添加了以下行:

[cell.theTextField becomeFirstResponder]; [cell.theTextField成为FirstResponder];

Which I believed should work. 我认为应该可以。 However, when the table view is first displayed, it does not seem to be working. 但是,首次显示表格视图时,它似乎无法正常工作。 BUT if I select the cell (making it the first responder), then go out of edit mode (causing a resignFirstResponder within my code), I can then go back into edit mode and magically it becomes the first responder as I would expect! 但是,如果我选择了该单元格(使其成为第一个响应者),然后退出了编辑模式(在我的代码中引起了resignFirstResponder ),那么我可以回到编辑模式,并且神奇地,它成为了我期望的第一个响应者!

Note that even if I end editing mode with a different cell selected (they all have text fields) and go out of edit mode, then back in, it still works, as long as at some point I had made the last cell becomeFirstResponder (by selecting it). 请注意,即使我结束选择了其他单元格的编辑模式(它们都具有文本字段)并退出编辑模式,然后再返回,它仍然有效, 只要在某个时候我已使最后一个单元格becomeFirstResponder (通过选择它)。

So, my guess is that when it first becomes the first responder, there is something getting set either in the table view or some place else that wasn't originally set, and from then on it makes this work. 因此,我的猜测是,当它第一次成为第一响应者时,在表视图中或其他最初未设置的地方都设置了某些东西,从那以后它开始起作用。

Anyone have any ideas as to what may be going on here? 有人对这里可能发生的事情有任何想法吗?

The first becomeFirstResponder call fails because the cell doesn't have a superview yet. 第一次的becomeFirstResponder调用失败,因为该单元还没有becomeFirstResponder视图。 The table view adds the cell as its subview after you return it from tableView:cellForRowAtIndexPath: . tableView:cellForRowAtIndexPath:返回单元格后,表格视图会将单元格添加为其子视图。 I suggest you make this call somewhere else. 我建议您在其他地方拨打此电话。 If you already have a custom UITableViewCell subclass, you could implement didMoveToWindow: and call [self becomeFirstResponder] there. 如果您已经有一个自定义UITableViewCell子类,则可以实现didMoveToWindow:然后在其中调用[self becomeFirstResponder]

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

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