繁体   English   中英

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

[英]Scrolling UITableView to top doesn't scroll properly

我的UIViewController拆分为2:

  1. 的UIView
  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;
}

您需要使控制器成为文本字段的委托,然后发送tableview scrollToRowAtIndexPath:atScrollPosition: animated:

看到这个确切的重复: 选择文本字段时使UITableView滚动

我有一个类似的问题,并且正如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");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM