我正在学习如何使用TableViews,我想知道如何判断tableView是向上还是向下滚动?我一直在尝试各种各样的事情,但是它没有被授予,下面是滚动视图,我有一个TableView。任何建议都会很棒,因为我是新手......
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0 {
print("down")
} else {
print("up")
}
}
这就是我在tableView代码中的内容
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
}
答案 0 :(得分:29)
就像@maddy在你的问题的评论中所说,你可以使用UITableView
检查你的UIScrollViewDelegate
是否正在滚动,而且你可以使用{scrollViewDidScroll
检查它滚动到哪个方向。{ 1}}和scrollViewWillBeginDragging
函数
// 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
}
}
此外:如果您已使用{UIViewController
已经将UIScrollViewDelegate
子类化,则不需要将UIViewController
与UITableViewDelegate
子类化1}}因为UITableViewDelegate
已经是UIScrollViewDelegate
答案 1 :(得分:25)
您可以实施scrollViewWillEndDragging
方法:
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if targetContentOffset.pointee.y < scrollView.contentOffset.y {
// it's going up
} else {
// it's going down
}
}
答案 2 :(得分:0)
首先,我们应该为“最后一个偏移”和“当前偏移”设置两个属性
...
<table class="table table-hover">
<tbody>
{% for h in todos_host %}
<tr class="thead-dark">
<th scope="row" width="30%">Check link: {{ h.hostname }}</th>
<td width="70%"><input class="form-control" name="checklink" type="text" placeholder="ex. http://xxxx"></td>
</tr>
{% endfor %}
</tbody>
</table>
...
然后在Scrollview VC中调用scrollViewWillBeginDragging获取当前偏移量
var lastOffset = CGFloat()
var currentOffset = CGFloat()
和scrollViewWillBeginDecelerating获取最后的偏移量
override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.lastOffset = scrollView.contentOffset.y
}
然后使用这两个变量,您可以知道滚动视图W 到这里了。
override func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
self.lastOffset = scrollView.contentOffset.y
viewStatus(scrollView: scrollView)
}