【问题标题】:UITableView keyboard resizing after insertSections not working在 insertSections 不起作用后 UITableView 键盘调整大小
【发布时间】:2013-05-12 07:39:20
【问题描述】:

我一直在使用表格视图,它允许用户在点击最后一个单元格“添加新项目”单元格时插入新数据。每个单元格内部都有一些文本字段,当其中一个成为第一响应者时,当然会出现键盘。我决定使用以下链接中的代码来动态调整 tableview 的大小,以防止键盘覆盖正在编辑的单元格。

Generic UITableView keyboard resizing algorithm

每次我点击之前添加的单元格中的任何文本字段时,这段代码都能完美运行,但是当我添加一个新单元格并以编程方式将其中的文本字段设置为第一响应者时,tableView 不会调整大小。我不是objective-c的专家,我真的不知道它出了什么问题。我将不胜感激。

这是用于添加新单元格的代码。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == self.dataController.countOfBudgetItemList)
{
    BudgetItem *newItem = [[BudgetItem alloc] initWithName:@"Name of item" price:0 quantity:1 quantityUnit:quantityUnitUnits discount:0 thumbnail:nil];

    [self.dataController addBudgetItemListObject:newItem];

    [CATransaction begin];

    [tableView beginUpdates];

    [CATransaction setCompletionBlock:
    ^{
        if(addingNewItem && indexPath.section == editingSectionNumber)
        {
            SectionTitleCell *addingNewItemTempCell = (SectionTitleCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:editingSectionNumber]];

            [addingNewItemTempCell.productNameTextField becomeFirstResponder];
        }

    }];

    if(editingSectionNumber >= 0)
    {
        [tableView reloadSections:[NSIndexSet indexSetWithIndex:editingSectionNumber] withRowAnimation:UITableViewRowAnimationFade];
    }

    addingNewItem = YES;
    editingSectionNumber = indexPath.section;

    [tableView insertSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];

    [tableView endUpdates];

    [CATransaction commit];
}
...
...
...
}

管理tableView中section个数和每行section个数的方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(editingSectionNumber == section)
{
    return 2;
}
else
{
    return 1;
}
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataController.countOfBudgetItemList + 1;
}

我也在使用 textFieldDidBeginEditing 方法,所以我不知道这是否会导致任何问题,因为触发 this 的事件和 keyboardWillShow 方法是相同的。

更新:

我修改了 animateWithDuration 方法调用以显示动画前后 tableView.frame 的值,我发现“动画”块中的值已正确修改,但动画完成后它会恢复为原始值.为什么框架会变回原来的大小?

[UIView animateWithDuration:animationDuration delay:delay options:UIViewAnimationOptionBeginFromCurrentState
        animations:
        ^{
            tableView.frame = tableFrame;

            NSLog(@"Origin: x=%f, y=%f", tableView.frame.origin.x, tableView.frame.origin.y);
            NSLog(@"Size: width=%f, height=%f", tableView.frame.size.width, tableView.frame.size.height);

            [self tableAnimationBegan];
        }
        completion:^(BOOL finished)
        {
            if(finished)
            {
                //self.budgetItemsTableView.scrollEnabled = NO;

                NSLog(@"Origin: x=%f, y=%f", tableView.frame.origin.x, tableView.frame.origin.y);
                NSLog(@"Size: width=%f, height=%f", tableView.frame.size.width, tableView.frame.size.height);
            }
        }];

【问题讨论】:

    标签: objective-c uitableview keyboard resize textfield


    【解决方案1】:

    好吧,我终于找到了解决问题的方法,所以我决定在这里发布,以防有​​人遇到同样的问题。

    基本上所有代码都可以正常工作,因为我使用了自动布局,我禁用了它,现在一切都像魅力一样工作。

    显然,当我以编程方式添加新单元格时,tableview 会自动调整大小,使其看起来我的代码完全没有做任何事情。

    【讨论】:

      猜你喜欢
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 2020-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多