swift - iOS tableview how can I check if it is scrolling up or down -
i learning how work tableviews , wondering how can figure out if tableview scrolling or down ? been trying various things such hasn't worked granted below scrollview , have tableview . suggestions great new @ ...
func scrollviewwillbegindragging(_ scrollview: uiscrollview) { if scrollview.pangesturerecognizer.translation(in: scrollview).y < 0 { print("down") } else { print("up") } }
this have in 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 }
like @maddy said in comment of question, can check if uitableview
scrolling using uiscrollviewdelegate
, further more check direction scrolls using both scrollviewdidscroll
, scrollviewwillbegindragging
functions
// set variable hold contentoffset before scroll view scrolls var lastcontentoffset: cgfloat = 0 // delegate called when scrollview (i.e uitableview) start scrolling func scrollviewwillbegindragging(scrollview: uiscrollview) { self.lastcontentoffset = scrollview.contentoffset.y } // while scrolling delegate being called may check direction scrollview being scrolled func scrollviewdidscroll(scrollview: uiscrollview) { if (self.lastcontentoffset < scrollview.contentoffset.y) { // moved top } else if (self.lastcontentoffset > scrollview.contentoffset.y) { // moved bottom } else { // didn't move } }
furthermore: don't need subclass uiviewcontroller
uiscrollviewdelegate
if you've subclassed uiviewcontroller
uitableviewdelegate
because uitableviewdelegate
subclass of uiscrollviewdelegate
Comments
Post a Comment