我在控制器中有CollectionView
和TableView
。我已segment control
CollectionView
scrollView
并在controller
中实施class MyViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate,UICollectionViewDelegateFlowLayout
委托,如下所示: -
scrollViewDidEndDecelerating
我有实现func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)
{
//here i want to detect which component scrolling(tableView or collectionView)
if scrollView.contentOffset.x == 0
{
segmentControl.selectedSegmentIndex = 0
DispatchQueue.main.async{
self.colView.reloadData()
}
}
else if scrollView.contentOffset.x == view.frame.size.width
{
DispatchQueue.main.async{
self.colView.reloadData()
}
segmentControl.selectedSegmentIndex = 1
}
DispatchQueue.main.async{
let segAttributes: NSDictionary = [
NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName: UIFont.systemFont(ofSize: 12)
]
let segAttributes1: NSDictionary = [
NSForegroundColorAttributeName: UIColor.init(red: 247.0/255.0, green: 105.0/255.0, blue: 8.0/255.0, alpha: 1),
NSFontAttributeName: UIFont.systemFont(ofSize: 12)
]
self.segmentControl.setTitleTextAttributes(segAttributes1 as [NSObject : AnyObject], for: UIControlState.selected)
self.segmentControl.setTitleTextAttributes(segAttributes as [NSObject : AnyObject], for: UIControlState.normal)
self.tblVIewAmenities.reloadData()
self.tblViewRoomDetails.reloadData()
}
}
}
,现在我想检测哪个组件滚动(tableView或collectionView)
SELECT test.* FROM
(select * from weather LEFT JOIN cities on cities.plz = weather.cities_plz
ORDER BY weather.modified DESC) as test
GROUP BY test.cities_plz
答案 0 :(得分:9)
由于UICollectionView
和UITableView
继承自UIScrollView
,您可以使用if let
尝试将UIScrollView
转换为其中任何一个,然后将显示它是:
extension ViewController: UIScrollViewDelegate {
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if let _ = scrollView as? UITableView {
print("tableview")
} else if let _ = scrollView as? UICollectionView {
print("collectionview")
}
}
}
或者,如果您为UITableView
和UICollectionView
存储了属性,则可以对传递给scrollView
的{{1}}使用相等性检查来确定哪个是:
scrollViewDidEndDecelerating(_:)
答案 1 :(得分:4)
UICollectionView和UITableView 继承自 UIScrollView ,因此您可以识别名称
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)
{
if scrollView == collectionViewName
{
// its in collectionview
}
else
{
// tableview
}
}
答案 2 :(得分:2)
我在Swift4上测试
func scrollViewDidScroll(_ scrollView: UIScrollView) {
switch scrollView {
case is UITableView: debugPrint("UITableView")
case is UICollectionView: debugPrint("UICollectionView")
default: break
}
}
答案 3 :(得分:0)
您可以使用 isDescendant 方法检查它,如果接收者是给定视图的子视图或与该视图相同,则该方法返回 true。假设我们有 tableView 和 collectionView 的引用。
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
if scrollView.isDescendant(of: tableView)
{
}
else if scrollView.isDescendant(of: collectionView)
{
}
}