我有一个UITableView,每个单元格内都有一个UITextField。存储当前正在编辑的单元格的索引的模型对象。如果单元格在屏幕外滚动,我的应用程序将取消第一响应者状态。 (不这样做可能会导致问题)。现在,假设对应于该索引的单元格(可能是相同的单元,或可能是不同的单元格)将要回滚到屏幕上。我想让那个单元格的textField成为第一个响应者。我的代表确实接到了电话
tableView: willDisplayCell: forRowAtIndexPath:
对应新细胞。但是,调用becomeFirstResponder:此时无效,因为单元格在显示之前不会接受firstResponder状态。
如果没有使用计时器,任何关于如何调用becomeFirstResponder的想法:在单元格实际上能够成为第一个响应者的时候?
编辑:cellForRowAtIndexPath:始终在willDisplayCell:之前调用。那里没有帮助。
答案 0 :(得分:1)
我没试过这个,但我要尝试的第一件事是在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;
}
答案 1 :(得分:0)
您必须在屏幕上显示单元格才能将其作为第一响应者。一开始做:
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
然后在它的标签/ textField上调用第一响应者。
答案 2 :(得分:0)
这是我在MonoTouch中所做的 - 重要的是你不要为ScrollToRow()设置动画 - 即“动画:否”,如edzio27的答案所示(感谢edzio27 :))。
var newCell = (UIOrderLineCell)tableView.CellAt(newIndexPath);
if (newCell == null)
{
tableView.ScrollToRow(newIndexPath, UITableViewScrollPosition.Middle, false);
newCell = (UIOrderLineCell)tableView.CellAt(newIndexPath);
}