gpt4 book ai didi

ios - 将 UITableView 滚动到顶部无法正确滚动

转载 作者:行者123 更新时间:2023-11-29 02:43:05 27 4
gpt4 key购买 nike

我的 UIViewController 拆分为 2:

  1. 界面 View
  2. UITableView

为了隐藏表格的其余部分,我在表格中添加了一个页脚 View 。由于我不能使用静态单元格,也不能隐藏表格的所有底部 View ,所以我做得有点棘手。

但是当我正确点击我的文本字段时,表格 View 没有滚动到顶部。

键盘隐藏了 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;
}

enter image description here

enter image description here

更新:

问题是, ScrollView 无法滚动,因为 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;
}

最佳答案

您需要让 Controller 成为您的文本字段的委托(delegate),然后发送 tableview scrollToRowAtIndexPath:atScrollPosition: animated:

请看这个完全相同的骗局:Making a UITableView scroll when text field is selected

关于ios - 将 UITableView 滚动到顶部无法正确滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25510252/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com