UITableView键盘在insertSections不工作后调整大小
问题描述:
我一直在使用一个表格视图,允许用户在最后一个单元格“添加新项目”单元被点击时插入新数据。每个单元格内部都有一些文本字段,当其中一个成为第一个响应者时,键盘将显示出来。我决定使用以下链接中的代码动态调整tableview的大小,以防止键盘覆盖正在编辑的单元格。UITableView键盘在insertSections不工作后调整大小
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段的数量和
-(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方法每一行中部分数量的方法,所以我不知道这是否可能会引发任何问题,因为触发这个事件和keyboardWillShow方法的事件是相同的。
UPDATE:
我修改了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);
}
}];
答
那么我终于找到了解决我的问题,所以我决定张贴在这里,以防有人正在经历相同的情况。
基本上所有的代码工作完全期望我使用autolayout的事实,我禁用它,现在一切都像一个魅力。
显然,当我以编程方式添加新单元格时,tableview被自动调整大小,使其看起来像我的代码完全没有。