如何让 scrollToRow 在键盘事件的 UITableView 中工作? [英] How do I get scrollToRow to work within a UITableView on keyboard events?

查看:27
本文介绍了如何让 scrollToRow 在键盘事件的 UITableView 中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图控制器中有一个 tableView 并且我有它,tableView 会根据键盘是在屏幕上还是屏幕外来收缩和扩展.当 tableView 缩小时,我希望它滚动到底行,但我无法让它工作.

I have a tableView in my view controller and I have it such that the tableView shrinks and expands depending on whether the keyboard is on the screen or off the screen. When the tableView shrinks, I want it to scroll to the bottom row but I can't get this to work.

目前,我有以下几点:

@objc func keyboardAppears(notification: Notification) {
        if let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect {
                bottomConstraint.constant = -1 * keyboardFrame.height
                view.loadViewIfNeeded()
                tableView.scrollToRow(at: IndexPath(row: 10, section: 0), at: .top, animated: true)
         }
}

我也试过把它放在主线程中.这使它在第一次键盘上升时起作用.代码如下:

I also tried putting it in the main thread. This got it to work the first time the keyboard went up only. The code looked as follows:

@objc func keyboardAppears(notification: Notification) {
        if let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect {
                bottomConstraint.constant = -1 * keyboardFrame.height
                view.loadViewIfNeeded()
                parentView.scrollUp()
         }
}

func scrollUp() {
        DispatchQueue.main.async {
            self.entrySpace.scrollToRow(at: IndexPath(row: self.data.count - 1, section: 0), at: .top, animated: true)
        }
}

更新:

在字段中输入一个字母后以下工作(我希望它在键盘出现时正常工作)

The following works after a letter is typed in the field (I want it to work right when the keyboard comes up)

注意:这是 UITableViewCell 类,因为单元格是 UITableView 中的最后一项

NOTE: this is the UITableViewCell class because the cell is the last item in the UITableView

import UIKit

class EntryButtonCell: UITableViewCell {
    
    //the textView for new entries
    var entryCell = UITextView()
    
    //Reference to the parent table
    var parentTable = UITableView()

    //Reference to the parent view (the one that holds the table view)
    var parentView = ParentVC()

    var bottomConstraint = NSLayoutConstraint()
    
    //Initialization
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //set this cell as the delegate for the entry textView
        entryCell.delegate = self
        
        //deactivate interaction with the cell so the user can interact with the textView
        contentView.isUserInteractionEnabled = false
        
        //set up the textView
        prepTextView()
        
        //set the notification for the keyboard events
        setKeyboardObservers()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //deinitialization
    deinit {
        //remove the keyboard observers
        removeKeyboardObservers()
    }
    
    func prepTextView() {
        //just did the constraints here 
        //removing to save space
    }
    
    func scrollToBottom() {
        DispatchQueue.main.async { [weak self] in
            guard let data = self?.parentView.data else { return }
            let indexPath = IndexPath(row: data.count, section: 0)
            self?.parentTable.scrollToRow(at: indexPath, at: .bottom, animated: true)
        }
    }
    
    //set the keyboard notification
    private func setKeyboardObservers() {
        //add keyboardWillShowNotification
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardAppears(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        
        //add keyboardWillHideNotification
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardDisappears(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    
    //deactivate the keyboard notifications
    private func removeKeyboardObservers() {
       //deactivate keyboardWillShowNotification
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
        
        //deactivate keyboardWillHideNotification
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    
    //run when keyboard shows
    @objc func keyboardAppears(notification: Notification) {
        if let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect {
            
                scrollToBottom()
                bottomConstraint.constant = -1 * keyboardFrame.height
                parentTable.layoutSubviews()
                parentView.loadViewIfNeeded()
        }
    }

    @objc func keyboardDisappears(notification: Notification) {
        tableBottomConstraint.constant = -100.0
        parentView.loadViewIfNeeded()
    }

    //Took out a few of the textView methods to save space
}

推荐答案

Swift 5

我已根据您的逻辑测试了以下代码,因此您只需添加几行即可使其工作.

Swift 5

I have tested the following code based on your logic so that you just add a few lines to get it to work.

// MARK: Keyboard Handling

@objc func keyboardWillShow(notification: Notification)
{
    if let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
    {
        tableView.reloadData()
        scrollToBottom()
        
        tableBottomConstraint.constant = -1 * (keyboardFrame.height + 100)
        loadViewIfNeeded()
    }
}

@objc func keyboardWillHide(notification: Notification)
{
    tableBottomConstraint.constant = -100.0
    loadViewIfNeeded()
}

func scrollToBottom()
{
    DispatchQueue.main.async { [weak self] in
        guard let data = self?.data else { return }
        let indexPath = IndexPath(row: data.count - 1, section: 0)
        self?.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
    }
}

结果

我的代码的输出将是这样的:

Result

The output of my code will be something like this:

这篇关于如何让 scrollToRow 在键盘事件的 UITableView 中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆