Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

swift - iOS tableview how can I check if it is scrolling up or down

I am learning how to work with TableViews and I am wondering how can I figure out if the tableView is scrolling up or down ? I been trying various things such as this but it hasn't worked granted that below is for a scrollview and I have a TableView . Any suggestions would be great as I am new at this ...

  func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0 {
        print("down")
    } else {
        print("up")
    }
}

This is what I have in my tableView code

func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
        return Locations.count
    }

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row == self.Posts.count - 4 {

            reloadTable(latmin: self.latmin,latmax: self.latmax,lonmin: self.lonmin,lonmax: self.lonmax,my_id: myID)
            print("Load More")
        }

    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "HomePageTVC", for: indexPath) as! NewCell


   cell.post.text = Posts[indexPath.row]
   cell.fullname.setTitle(FullName[indexPath.row],for: UIControlState.normal)

        return cell
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Like @maddy said in the comment of your question, you can check if your UITableView is scrolling by using the UIScrollViewDelegate and further more you could check which direction it scrolls to by using both scrollViewDidScroll and scrollViewWillBeginDragging functions

// we set a variable to hold the contentOffSet before scroll view scrolls
var lastContentOffset: CGFloat = 0

// this delegate is called when the scrollView (i.e your UITableView) will start scrolling
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    self.lastContentOffset = scrollView.contentOffset.y
}

// while scrolling this delegate is being called so you may now check which direction your scrollView is being scrolled to
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if self.lastContentOffset < scrollView.contentOffset.y {
        // did move up
    } else if self.lastContentOffset > scrollView.contentOffset.y {
        // did move down
    } else {
        // didn't move
    }
}

Furthermore: You don't need to subclass your UIViewController with UIScrollViewDelegate if you've already subclassed your UIViewController with UITableViewDelegate because UITableViewDelegate is already a subclass of UIScrollViewDelegate


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...