Iphone UITableView无法正确滚动(或根本无法滚动)
我试图在UITableView中滚动,同时显示键盘。我调整了TableView的大小,使其不被键盘覆盖。我重写了textFieldShouldReturn函数,以跳到UITableView中的下一个UITextField,并滚动到下一个字段。以下是我使用的代码:Iphone UITableView无法正确滚动(或根本无法滚动),iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,我试图在UITableView中滚动,同时显示键盘。我调整了TableView的大小,使其不被键盘覆盖。我重写了textFieldShouldReturn函数,以跳到UITableView中的下一个UITextField,并滚动到下一个字段。以下是我使用的代码: - (BOOL)textFieldShouldReturn:(UITextField *)textField { UITableViewCell *cell = (UITableViewCell *)[textField sup
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
UITableViewCell *cell = (UITableViewCell *)[textField superview];
NSIndexPath *indexPath = [self.prefTableView indexPathForCell:cell];
NSString *nextRowKey = [preferences nextRowKeyAtIndexPath:indexPath];
[textField resignFirstResponder];
if (nextRowKey != nil) {
tagBeingEdited = textField.tag + 1;
NSIndexPath *nextIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1
inSection:indexPath.section];
[self.prefTableView scrollToRowAtIndexPath:nextIndexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
//[self.prefTableView selectRowAtIndexPath:nextIndexPath
// animated:YES
// scrollPosition:UITableViewScrollPositionMiddle];
if ([[self.prefTableView indexPathsForVisibleRows] containsObject:indexPath] == YES) {
[[self.prefTableView viewWithTag:tagBeingEdited] becomeFirstResponder];
}
} else {
isEditing = NO;
}
return NO;
}
但是,我的视图从不滚动。或者,如果我手动滚动视图,它将返回顶部。为什么?
注意:我只使用ScrollToRowatineXpath和SelectRowatineXpath中的一种。我只是把它们都放在那里,因为我先尝试了一个,然后又尝试了另一个。@occulus是正确的,UITableViewController会在键盘出现时自动处理大小调整。如果希望UIViewController的子类具有这种行为,则需要额外的工作 下面来自Cocoa with love的链接有一个这样做的例子
您的视图控制器是UIViewController的子类吗?如果改用UITableViewController子类,则可以更好地支持键盘+表格协同工作。例如,它将为您处理UITableView的大小调整。它是UITableViewController的一个子类。不过,巴哈维奥和现在一样。在其他提示后,我将其更改为UIViewController。这是我的视图控制器的声明:@interface PreferencesController:UIViewController我尝试了更多,添加了更多行,并启用了单元格来显示它们的选择。选择正确进行,但视图几乎任意滚动。好的,我隔离了问题。我的UITableView和UITableViewController对是另一个UIViewController派生对象的子视图。我手动加载自己的类,并使用addSubView将其添加到主UIView。如果我省略了这个中间类,滚动和调整大小会自动工作。但是,我希望有一个中间类,因为我添加了一个标题和一个按钮来关闭视图。。。让我们看看我能不能让他们一起工作