简体   繁体   English

在 iOS 15 表格视图中滚动到顶部

[英]Scroll to top in iOS 15 table view

How do you scroll to the top of a table view in iOS 15?如何在 iOS 15 中滚动到表格视图的顶部? In previous versions, I was using the -scrollToTop<\/code> method, but in iOS 15 this scrolls past the padding of the first table section header.在以前的版本中,我使用的是-scrollToTop<\/code>方法,但在 iOS 15 中,它会滚动到第一个表格节标题的填充之外。

"

UITableViews does not contain any method called 'scrollToTop' or 'scrollToBottom' but you can create an extension for tableview and create these methods : UITableViews 不包含任何名为“scrollToTop”或“scrollToBottom”的方法,但您可以为 tableview 创建扩展并创建这些方法:

extension UITableView {

    func scrollToBottom(isAnimated:Bool = true){

        DispatchQueue.main.async {
            let indexPath = IndexPath(
                row: self.numberOfRows(inSection:  self.numberOfSections-1) - 1,
                section: self.numberOfSections - 1)
            if self.hasRowAtIndexPath(indexPath: indexPath) {
                self.scrollToRow(at: indexPath, at: .bottom, animated: 
                isAnimated)
            }
         }
     }

    func scrollToTop(isAnimated:Bool = true) {

        DispatchQueue.main.async {
            let indexPath = IndexPath(row: 0, section: 0)
            if self.hasRowAtIndexPath(indexPath: indexPath) {
                self.scrollToRow(at: indexPath, at: .top, animated: 
                isAnimated)
            }
        }
    }

    func hasRowAtIndexPath(indexPath: IndexPath) -> Bool {
        return indexPath.section < self.numberOfSections && 
        indexPath.row < self.numberOfRows(inSection: indexPath.section)
    }
}

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

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