在UITextField成为第一响应者后,UITableView不会滚动到可见状态
问题描述:
我有一个加载了自定义单元格的UITableView
。这些单元有两种可能的状态:只读状态和编辑状态。我把它们之间的改变通过点击相应的行表视图:在UITextField成为第一响应者后,UITableView不会滚动到可见状态
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomClass *aCustomClass = [self.model objectAtIndex:indexPath.row];
[aCustomClass setEdition:YES];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
的问题是,我需要点击行后设置单元格的UITextField
作为第一个响应者。我所做的就是添加以下代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
if (!cell)
cell = ... // Create cell
...
[cell.textField becomeFirstResponder];
}
一切运作良好,但是当UITextField
成为第一个响应我想表视图整个单元滚动显示。对于这一点,我实现了这样的键盘通知事件:
- (void)keyboardWillBeShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGRect kbRawRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect ownFrame = [self.tableView.window convertRect:self.tableView.frame fromView:self.tableView.superview];
// Calculate the area that is covered by the keyboard
CGRect coveredFrame = CGRectIntersection(ownFrame, kbRawRect);
coveredFrame = [self.tableView.window convertRect:coveredFrame toView:self.tableView.superview];
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, coveredFrame.size.height, 0.0);
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
[self.tableView scrollToRowAtIndexPath:self.openCellIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
的问题是,表视图不会细胞滚动到顶部位置,最后一行表示。
如果我删除自动拨打becomeFirstResponder
并点击UITextField
手动,所有工作正常。
你知不知道为什么会发生这种情况,我该如何解决这个问题?
感谢,
答
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, coveredFrame.size.height, 0.0);
应该改变这样的:
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, -coveredFrame.size.height, 0.0);
+0
可能要详细说明原因以及上述如何修复它。 – Leigh
为什么你不加[cell.textField becomeFirstResponder]在didselectrowAtIndexPath而不是在cellForRowAtIndexPath中。 –
这不起作用。当我希望整个单元格可见时,只有文本字段可见。谢谢。 –
你不能添加TextField和整个单元格之间的差异像素到你的计算,所以整个单元格是可见的吗? – Jasper