【发布时间】:2014-10-20 00:58:13
【问题描述】:
我的 UIViewController 拆分为 2:
- UI 视图
- UITableView
我在表格中添加了页脚视图以隐藏表格的其余部分。 因为我不能使用静态单元格,也不能隐藏表格的所有底部视图,所以我做的有点棘手。
但是当我正确点击我的文本字段时,表格视图没有滚动到顶部。
键盘隐藏了 UITextField 并且没有滚动到正确的点。
我该如何解决?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier;
if (indexPath.row == 0) identifier = @"cell_name";
else if (indexPath.row == 1) identifier = @"cell_password";
else if (indexPath.row == 2) identifier = @"cell_password_verify";
else if (indexPath.row == 3) identifier = @"cell_email";
else if (indexPath.row == 4) identifier = @"cell_cellphone";
else if (indexPath.row == 5) identifier = @"cell_social";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
return cell;
}
- (void)configureUI
{
UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 1)];
tableFooterView.backgroundColor = [UIColor whiteColor];
self.tableView.tableFooterView = tableFooterView;
tableFooterView.backgroundColor = [UIColor whiteColor];
self.tableView.tableFooterView = tableFooterView;
self.edgesForExtendedLayout = UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars = NO;
self.automaticallyAdjustsScrollViewInsets = NO;
}
更新:
问题是,滚动视图无法滚动,因为tableFooterView 太短,然后我修改了我的代码。
基本上,@Roger Nolan 对,我还添加了以下代码,现在它可以完美运行:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UITableViewCell *cell = (UITableViewCell *)textField.superview.superview.superview;
[self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- (void)registerObservers
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
}
- (void)keyboardDidShow:(NSNotification *)notification
{
CGFloat keyboardHeight = [CoreGraphicsHandler keyboardFramFromNotification:notification].size.height;
UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), keyboardHeight)];
tableFooterView.backgroundColor = [UIColor whiteColor];
self.tableView.tableFooterView = tableFooterView;
}
- (void)keyboardDidHide:(NSNotification *)notification
{
UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 1)];
tableFooterView.backgroundColor = [UIColor whiteColor];
self.tableView.tableFooterView = tableFooterView;
}
【问题讨论】:
-
当键盘显示时,可以修改顶部UIView的边距吗?也许我没有正确理解这个问题。
-
我编辑了我的问题。
-
我遇到了这个确切的问题,我只有解决方案,但我得等到今晚回家后再分享。
-
看起来您正在使用动态委托方法实现静态表视图。您应该将其设为静态表格视图。在 IB 中,选择表格视图,它是属性编辑器中的顶部项目。
标签: ios objective-c uitableview uiscrollview