【问题标题】:Scrolling UITableView to top doesn't scroll properly将 UITableView 滚动到顶部无法正确滚动
【发布时间】:2014-10-20 00:58:13
【问题描述】:

我的 UIViewController 拆分为 2:

  1. UI 视图
  2. 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


【解决方案1】:

您需要让控制器成为文本字段的代表,然后发送 tableview scrollToRowAtIndexPath:atScrollPosition: animated:

看到这个确切的欺骗:Making a UITableView scroll when text field is selected

【讨论】:

  • 它调用的委托方法不滚动表格。
  • 您的表格视图 iVar 设置是否正确?如果您正确调用该方法,它将滚动。
  • 基本上你是对的,我没有注意到我的 'tableFooterView' 太短,我更新了我的问题。
【解决方案2】:

我遇到了类似的问题,正如 Idan Moshe 在更新中所建议的,我解决了在 tableView 中插入 footerView 的问题。 Swift 4 中的解决方案:

func registerObservers(){

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: NSNotification.Name.UIKeyboardDidShow, object: nil);
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidHide), name: NSNotification.Name.UIKeyboardDidHide, object: nil);

}

@objc func keyboardDidShow(_ notification: Notification) {

    DispatchQueue.main.async {

        if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
            let keyboardHeight = keyboardFrame.cgRectValue.height;
            let tableFooterView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: keyboardHeight))
            self.tableView.tableFooterView = tableFooterView;
        }
    }
}

@objc func keyboardDidHide(_ notification: Notification) {

    DispatchQueue.main.async {

        let tableFooterView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 1));
        self.tableView.tableFooterView = tableFooterView;
    }
}

func textFieldDidBeginEditing(_ textField: UITextField) {

    let cell = self.selectedTExtField?.superview?.superview;
    if let idxPath = self.tableView.indexPath(for: cell){
        self.tableView.scrollToRow(at: idxPath, at: .top, animated: true);
    }
    else{
        print("Error: index path not found");
    }
}

【讨论】:

    猜你喜欢
    • 2010-10-18
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 2019-11-14
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    相关资源
    最近更新 更多