简体   繁体   English

iOS TableView上的Scrollview事件问题

[英]iOS TableView on Scrollview Event Problem

I have a scrollview in a ViewController. 我在ViewController中有一个scrollview。 The scrollview has a UIScrollViewDelegate .On upon that i am adding a Tableview like below: scrollview有一个UIScrollViewDelegate 。在此之上,我正在添加一个Tableview如下所示:

self.view.addSubview(self.tableView)

But when i am scrolling on tableview cells somehow the scrollview delegate is getting event, which is unexpected. 但是,当我以某种方式在tableview单元上滚动时,scrollview委托正在获取事件,这是意外的。 What can i do to prevent that? 我该怎么做才能防止这种情况?

The UITableView is the subclass of UIScrollView and that is the reason you are getting calls to the UIScrollViewDelegate when you are scrolling the cell s. UITableViewUIScrollView的子类,这就是在滚动cell s时获得对UIScrollViewDelegate调用的原因。

You can not prevent this if you want to have the scrolling enabled for the TableView . 如果要为TableView启用滚动,则无法避免这种情况。 I would suggest you check this condition inside your delegate method, for example: 我建议您在委托方法中检查此条件,例如:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    guard scrollView != self.tableView else {return}
}

That's normal because UITableView is a subclass of UIScrollView. 这是正常的,因为UITableView是UIScrollView的子类。 Thus you should check if scrollView != self.tableView then do what you want to do inside the scrollView delegate method. 因此,您应该检查scrollView!= self.tableView,然后在scrollView委托方法内执行您想做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM