UITableViewCell中的UITextField - 以编程方式使它在屏幕上出现firstResponder?
我有一个UITableView与每个单元格内的UITextField。存储当前正在编辑的单元格索引的模型对象。如果单元格在屏幕外滚动,我的应用程序就会取消第一响应者状态。 (如果不这样做可能会导致问题)。现在,假设与该索引对应的单元格(可能是同一个单元格,或可能是不同的单元格)即将滚动回屏幕上。我想让单元格的textField成为firstResponder。我的代表确实收到呼叫UITableViewCell中的UITextField - 以编程方式使它在屏幕上出现firstResponder?
tableView: willDisplayCell: forRowAtIndexPath:
对应于新的小区。但是,调用becomeFirstResponder:此时不起作用,因为单元在显示之前不会接受firstResponder状态。
使用计时器的缺点,如何调用becomeFirstResponder的任何想法:在细胞实际上能够成为第一响应者的时候?
编辑:cellForRowAtIndexPath:总是在willDisplayCell:之前调用。所以没有帮助。
我没有试过,但我想尝试的第一件事是的cellForRowAtIndexPath ...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// standard stuff to build cell and setup it's state
if ([indexPath isEqual:self.myModel.indexPathOfTextFieldBeingEdited]) {
// you probably have a handle to the text field from the setup above
UITextField *textField = (UITextField *)[cell viewWithTag:SOME_TAG];
[textField performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
}
return cell;
}
试过。不好。问题仍然是该单元格未显示。 – 2013-03-03 19:22:04
你使用了performSelector吗?在cellForRowAtIndexPath已经返回并且主循环已经返回之后,它肯定已经显示。 – danh 2013-03-03 19:23:01
这实际上是一个计时器。这样的延迟通常会起作用。但这不是理想的编程风格,可能会被Apple更新所破坏。 – 2013-03-03 22:56:43
你必须在屏幕上显示细胞,使其作为第一个响应。先做:
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
然后调用它的标签/ textField上的第一响应者。
下面是我在MonoTouch中所做的工作 - 重要的是您不要为ScrollToRow()设置动画效果 - 即如edzio27(谢谢edzio27 :)答案中所示的“animated:NO”)。
var newCell = (UIOrderLineCell)tableView.CellAt(newIndexPath);
if (newCell == null)
{
tableView.ScrollToRow(newIndexPath, UITableViewScrollPosition.Middle, false);
newCell = (UIOrderLineCell)tableView.CellAt(newIndexPath);
}
任何时候细胞appeares -cellForRowAtIndexPath委托方法被调用,所以,你是否尝试设置uitextfield作为第一响应者? – alex 2013-03-03 19:04:26