Swift 为什么我的表视图只滚动一部分?

Swift 为什么我的表视图只滚动一部分?,swift,uitableview,uiscrollview,uiscrollviewdelegate,Swift,Uitableview,Uiscrollview,Uiscrollviewdelegate,我正在实现一个UITableView,我希望一旦用户开始滚动,表视图就会自动向上滚动到顶部。我使用了类似的逻辑来检测用户滚动的方向。当用户开始向上滚动时,下面的函数会检测到滚动,但是table.scrollToRowat:IndexPathrow:0,section:0,at:.top,animated:true会非常缓慢地移动,并在单元格顶部停止一部分。我怎样才能解决这个问题 func scrollViewDidEndDragging(_ scrollView: UIScrollView, w

我正在实现一个UITableView,我希望一旦用户开始滚动,表视图就会自动向上滚动到顶部。我使用了类似的逻辑来检测用户滚动的方向。当用户开始向上滚动时,下面的函数会检测到滚动,但是table.scrollToRowat:IndexPathrow:0,section:0,at:.top,animated:true会非常缓慢地移动,并在单元格顶部停止一部分。我怎样才能解决这个问题

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    print("Dragging has ended")
    //print("lastContentOffSet: \(self.lastContentOffset)")
    if (self.lastContentOffset < scrollView.contentOffset.y) {
        // moved to top
        print("Scrolled down")
    } else if (self.lastContentOffset > scrollView.contentOffset.y) {
        // moved to bottom
        print("Scrolled up")
        table.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
    } else {
        // didn't move
    }
}
下面是完整的代码

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var table:UITableView!
    @IBOutlet weak var photosButton: UIButton!

    var lastContentOffset: CGFloat = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        table.delegate = self
        table.dataSource = self 
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return  2
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell:UITableViewCell = UITableViewCell()
        if indexPath.row == 1{
            cell = table.dequeueReusableCell(withIdentifier: "cameraCell", for: indexPath) as! cameraCell
            cell.backgroundColor = .red
        }else{
            cell = table.dequeueReusableCell(withIdentifier: "photoAlbumCell", for: indexPath) as! photoAlbumCell
            cell.backgroundColor = .blue
        }
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UIScreen.main.bounds.height
    }
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        print("Scrolling began")
        self.lastContentOffset = scrollView.contentOffset.y
    }
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        print("Dragging has ended")
        if (self.lastContentOffset < scrollView.contentOffset.y) {
            // moved to top
            print("Scrolled down")
        } else if (self.lastContentOffset > scrollView.contentOffset.y) {
            // moved to bottom
            print("Scrolled up")
            table.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
        } else {
            // didn't move
        }
    }
    @IBAction func photoAlbumButton(_ sender: UIButton) {
        table.scrollToRow(at: IndexPath(row: 1, section: 0), at: .top, animated: true)
    }
}

class cameraCell:UITableViewCell{
    override func awakeFromNib() {
        super.awakeFromNib()
    } 
 }
class photoAlbumCell: UITableViewCell {
    override func awakeFromNib() {
        super.awakeFromNib()
    }
}

针对上述问题,有两种解决方案-

解决方案1-func ScrollViewDiEndDraging_scrollView:UIScrollView,将减速减速减速:当用户结束拖动时,必须处理布尔。要处理正常滚动并在减速时离开,请使用func scrollViewDiEndDecelling\uScrollView:UIScrollView。请注意,您需要这两种委托方法来处理滚动。两个代理将执行同一组代码。唯一的变化是第一个委托执行代码的方式,即,您需要添加!减速条件,因为我们正在处理减速场景的第二个代理。请注意,抬起手指后,它会缓慢滚动,直到调用代理,但它会确保滚动到顶部。换句话说,您的代码应该如下所示-

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    print("Dragging has ended")
    if (self.lastContentOffset < scrollView.contentOffset.y) {
        // moved to top
        print("Scrolled down")
    } else if (self.lastContentOffset > scrollView.contentOffset.y) {
        // moved to bottom
        print("Scrolled up")
        table.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
    } else {
        // didn't move
    }
}

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    if !decelerate { // decelerate logic will be handled above
        print("Dragging has ended")
        if (self.lastContentOffset < scrollView.contentOffset.y) {
            // moved to top
            print("Scrolled down")
        } else if (self.lastContentOffset > scrollView.contentOffset.y) {
            // moved to bottom
            print("Scrolled up")
            table.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
        } else {
            // didn't move
        }
    }
}
我个人更喜欢第一种解决方案,因为这是一种可以正确处理拖动和减速情况的方法。

我添加了ScrollViewWillbegindelerating,以及与您的解决方案中相同的代码,效果非常好,非常感谢您的帮助。
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    print("Dragging has ended")
    if (self.lastContentOffset < scrollView.contentOffset.y) {
        // moved to top
        print("Scrolled down")
    } else if (self.lastContentOffset > scrollView.contentOffset.y) {
        // moved to bottom
        print("Scrolled up")
        DispatchQueue.main.async {
            self.table.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
        }

    } else {
        // didn't move
    }
}