【问题标题】:Scroll UITableView frame problems to see UITextField in cell?滚动 UITableView 框架问题以查看单元格中的 UITextField?
【发布时间】:2012-02-28 21:19:38
【问题描述】:

在我的应用程序中,我有一个 UITableView,其中包含一些自定义单元格,并且在其中放置了 UITextField。 UITextField 有时会被我的键盘挡住,这是一个问题。所以在我的情况下,我试图移动整个 tableview 本身,而不是仅仅滚动到对我来说是个问题的特定单元格。

反正我也遇到这种问题。

无论如何,这是我的 textField 显示方法和结束方法的代码。问题是 UITableView 永远不会移动正确的数量。我只是想将 UITableview 向上移动到足以使当前正在调用的 UITextField 的单元格就在键盘上方。 所以有时它会在我看不到单元格的地方滚动太多,或者整个表格视图在我根本看不到它的地方移动了一个疯狂的量。

这里是我的代码,有没有人发现有什么问题?

- (BOOL)textFieldShouldBeginEditing:(UITextField *)thetextField {
    CustomCell *cell = (CustomCell*)[[thetextField superview] superview];
    CGRect cellRect = [cell convertRect:cell.frame toView:self.view];
    float bottomCell = cellRect.origin.y - cellRect.size.height;
    if (bottomCell >= keyboardY) {
        subtractionAmount = keyboardY - bottomCell;
        didMove = YES;
        [UIView animateWithDuration:0.4 
                         animations:^{
                             thetableView.center = CGPointMake(thetableView.center.x, thetableView.center.y + subtractionAmount);
                         }];
    }
    return YES;
}

-(BOOL) textFieldShouldReturn:(UITextField *)thetextField {  
    if (didMove) {
        didMove = NO;
        [UIView animateWithDuration:0.4 
                         animations:^{
                             thetableView.center = CGPointMake(thetableView.center.x, thetableView.center.y - subtractionAmount);
                         }];
    }

谢谢!

【问题讨论】:

    标签: ios uitableview keyboard uitextfield frame


    【解决方案1】:

    如果我没有正确理解您的问题,我在一个月前就遇到了这个问题。我的场景如下:UITableViewUITextFields 没有 UITableViewController

    我通过以下教程解决了这个问题:sliding-uitextfields-around-to-avoid

    希望对你有帮助。

    【讨论】:

    • 从表面上看,我正在做本教程中的所有事情,但只是有点不同,所以我仍然很好奇为什么上面的代码不能完全工作。
    猜你喜欢
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 2016-05-20
    • 2011-08-10
    相关资源
    最近更新 更多