将UITableView滚动到顶部不会正确滚动

时间:2014-08-26 16:00:48

标签: ios objective-c uitableview uiscrollview

我的UIViewController拆分为2:

  1. 的UIView
  2. 的UITableView
  3. 我已向表格添加了页脚视图,以隐藏表格的其余部分。 由于我不能使用静态单元格并且还隐藏了表格的所有底部视图,所以我做得有点棘手。

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

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

    更新

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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