【问题标题】:How to UITableView keep Scrolling to bottom [Auto Scrolling]?UITableView如何保持滚动到底部[自动滚动]?
【发布时间】:2019-05-10 10:11:58
【问题描述】:

我花了很多时间来找出这个错误。在开始滚动 看起来不错,但不时滚动增加自动 人类无法阅读。如何保持滚动与开始一样。

    var  start_index = 0
    var  dest_index  = 0  //approximately 300  
    var  timer       = Timer()
    var  falagisTrue : Bool?

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        dest_index = newsViewModels.count
        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.keepScrolling), userInfo: nil, repeats: true)

    }



    @objc func keepScrolling(){
        if start_index > dest_index - 2{
            timer.invalidate()
        }else{
            if  (falagisTrue ?? false ||  falagisTrue == nil)  {
                falagisTrue = false
                start_index += 1

                UIView.animate(withDuration: 10, delay: 0, options: [], animations: {
                    let Index = IndexPath(row: self.start_index, section: 0)
                    self.tableView.scrollToRow(at:Index, at: .top, animated: false)
                }) { finished in
                    self.falagisTrue = true
                }
            }
        }
    }

请您帮我修复这个错误或提供任何替代解决方案以保持自动滚动?

【问题讨论】:

    标签: swift uitableview smooth-scrolling


    【解决方案1】:

    我尝试了您的代码,它对我来说可以正常工作。但是:

    1. 您可以添加动画选项 .curveLiner(动画将在整个持续时间内均匀发生)。可能是你的错误:

      UIView.animate(withDuration: 10, delay: 0, options: [.curveLinear], animations: {
          let Index = IndexPath(row: self.start_index, section: 0)
          self.tableView.scrollToRow(at:Index, at: .top, animated: false)
      }) { finished in
          self.falagisTrue = true
      }
      
    2. 当视图消失时,您应该使定时器无效(不同的是,视图在旧定时器工作时每次都会创建额外的定时器):

      override func viewWillDisappear(_ animated: Bool) {
          timer.invalidate()
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      相关资源
      最近更新 更多