2

我有一个带有自定义单元格的分组表视图,这些单元格有一个UITextField. 我已将returnKeyType文本字段的 设置为UIReturnKeyNext(最后一行除外),并且我以textFieldShouldReturn:这种方式实现了他们的方法:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
   id firstResponderCell = textField.superview.superview ;
   NSInteger firstResponderRow = [self.tableView indexPathForCell:firstResponderCell].row ;
   NSIndexPath* nextCellIndexPath = [NSIndexPath indexPathForRow:firstResponderRow+1 inSection:0] ;
   CustomCell* nextCell = (CustomCell*)[self.tableView cellForRowAtIndexPath:nextCellIndexPath] ;

   if (nextCell) {
      [nextCell.cellTextField becomeFirstResponder] ;
   }
   else
      [textField resignFirstResponder] ;
   return YES ;

}

但是我无法滚动表格视图内容以使当前活动的文本字段在键盘上方可见。当我没有在键盘中实现“下一步”按钮功能时,我成功地管理了这个,当点击“Enter”按钮时,键盘只是隐藏了,然后在点击另一个文本字段时再次显示键盘,因为我正在听UIKeyboardDidShowNotificationUIKeyboardDidHideNotification。但是现在我想要键盘中的“下一步”按钮,并且在点击按钮时键盘没有被隐藏,我不知道如何正确滚动到活动文本字段。

提前致谢

4

2 回答 2

2

这是实现这一目标的最简单有效的方法:

添加以下常量:

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;

将此添加到您的视图控制器:

CGFloat animatedDistance;

并将这些方法添加到您的代码中:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect =
[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
    heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
    heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
    orientation == UIInterfaceOrientationPortraitUpsideDown)
{
    animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
    animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

[self.view setFrame:viewFrame];

[UIView commitAnimations];

}

- (void)textFieldDidEndEditing:(UITextField *)textField
    {
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

[self.view setFrame:viewFrame];

[UIView commitAnimations];

}

PS:这个来自这里

于 2013-12-04T06:01:38.283 回答
0

这不是获取单元格的安全方法:

id firstResponderCell = textField.superview.superview ;

即它现在可能有效,但在(不久的)将来可能无效。

知道索引路径后,您应该检查它是否确实存在。然后您可以使用scrollToRowAtIndexPath:atScrollPosition:animated:滚动行(并使其成为顶行)。或者,您可以获取行的框架,然后设置contentOffset表格视图的框架。

于 2013-08-18T11:11:24.053 回答