【问题标题】:how to go to the top of a tableView on button press如何在按钮按下时转到 tableView 的顶部
【发布时间】:2019-07-27 01:59:59
【问题描述】:

我正在使用 swift,我想在按下按钮时转到 tableView 的顶部,就像在 twitter 上按下主页按钮会将你带到你的提要的顶部,到目前为止我还不能找到一个可行的解决方案,这是我尝试过的:

tableView.setContentOffset(.zero, animated: true)

tableView.setContentOffset(CGPoint(x: 0.0, y: -tableView.contentInset.top), animated: true)

func scrollToTop(){
        let indexPath = IndexPath(row: 0, section: 0)
        tableView.scrollToRow(at: indexPath, at: .top, animated: true)
    }

所有解决方案只会将您带到任意单元格,如果您也知道在标签栏按钮单击时执行此操作的解决方案,则可以加分

【问题讨论】:

标签: ios swift uitableview


【解决方案1】:

首先,我喜欢你的用户名。

接下来,您发布的解决方案又有什么问题?他们是正确的,除了scrollTo... 内置函数之外,似乎没有比UITableViewUICollectionView 更好的方法了。并将offset 设置为UIScrollView

对于标签栏上点击的处理,你可以做的一件事是继承UITabBarController,并符合其协议,并实现UITabBarControllerDelegate所需的方法,例如:

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {.

您可以从那里处理所有事情,然后从那里获取viewController 对象并将其投射到您的家庭类中,然后将视图滚动到顶部。

我为此使用什么功能?和你的差不多,但是有一点条件:

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

func scrollToTop(animated: Bool) {
    let indexPath = IndexPath(row: 0, section: 0)
    if self.hasRowAtIndexPath(indexPath: indexPath) {
        self.scrollToRow(at: indexPath, at: .top, animated: animated)
    }
}

此外,请尝试点击应用的状态栏,看看它会将您带到哪里。

【讨论】:

  • 状态栏有效,但我尝试的那些解决方案只会占用几个单元格,而不是一直到顶部
  • 如果有表格视图标题和/或部分标题,则不会滚动到顶部。
  • @Stopdownvotingmeplease 你试过这个吗? : tableView.scrollRectToVisible(CGRect(x: 0, y: 0, width: tableView.frame.width, height: tableView.frame.height), 动画: true)
【解决方案2】:

我再次编辑了我的答案,没有涉及 indexPath:

@IBOutlet weak var myButton: UIButton!

override func viewDidLoad() { 
  super.viewDidLoad
}


@IBAction func button(_ sender: UIButton) { 

  myButton.isSelected = !myButton.isSelected
  tableView.scrollRectToVisible(CGRect(x: 0, y: 0, width: tableView.frame.width, height: tableView.frame.height), animated: true) 

}

 override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {

    if myButton.isSelected {

        print("SUCCESS 1")


    }
}

override func scrollViewWillEndDragging(_ scrollView: UIScrollView,   withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    if myButton.isSelected {
        myButton.isSelected = false
        print("SUCCESS 2")

    }
}

【讨论】:

  • 如果有表格视图标题和/或部分标题,则不会滚动到顶部。
【解决方案3】:

我猜问题是按钮函数中的一些其他逻辑与 scrollToTop() 函数混淆了,这段代码有效:

func scrollToTop(){
        tableView.setContentOffset(.zero, animated: true)
    }

    @IBAction func BlogPressed(_ sender: Any) {

        if(BlogIndicator.alpha == 1.0){
            scrollToTop()
        } else {
            articleRefresh()
            self.articleArray = self.blogArray

            BlogIndicator.alpha = 1.0
            NewsIndicator.alpha = 0.0
            RosterIndicator.alpha = 0.0
            DraftIndicator.alpha = 0.0

            tableView.reloadData()
        }
    }

但我仍在寻找一种解决方案,以便在按下标签栏按钮时转到表格视图的顶部

【讨论】:

    猜你喜欢
    • 2019-01-28
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    • 2018-09-13
    相关资源
    最近更新 更多