检测同一控制器中的 TableView 和 CollectionView 滚动
Posted
技术标签:
【中文标题】检测同一控制器中的 TableView 和 CollectionView 滚动【英文标题】:Detect TableView and CollectionView scrolling in same controller 【发布时间】:2017-04-25 13:23:24 【问题描述】:我在控制器中有CollectionView
和TableView
。我通过CollectionView
制作了segment control
并在controller
中实现scrollView
delegate,如下所示:-
class MyViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate,UICollectionViewDelegateFlowLayout
我已经实现了scrollViewDidEndDecelerating
,现在我想检测哪个组件滚动(tableView 或 collectionView)
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()
【问题讨论】:
带标签可以检测哪个组件在滚动 【参考方案1】:由于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
的scrollView
使用相等性检查来确定哪个是哪个:
class ViewController: UIViewController
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var tableView: UITableView!
extension ViewController: UIScrollViewDelegate
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)
if scrollView == tableView
print("tableview")
else if scrollView == collectionView
print("collectionview")
【讨论】:
【参考方案2】:UICollectionView 和 UITableView 继承自 UIScrollView,所以可以用名字来识别
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)
if scrollView == collectionViewName
// its in collectionview
else
// tableview
【讨论】:
【参考方案3】:我在 Swift4 上测试过
func scrollViewDidScroll(_ scrollView: UIScrollView)
switch scrollView
case is UITableView: debugPrint("UITableView")
case is UICollectionView: debugPrint("UICollectionView")
default: break
【讨论】:
【参考方案4】:您可以使用 isDescendant 方法检查它,如果接收者是给定视图的子视图或与该视图相同,则返回 true。假设我们有 tableView 和 collectionView 的引用。
func scrollViewWillBeginDragging(_ scrollView: UIScrollView)
if scrollView.isDescendant(of: tableView)
else if scrollView.isDescendant(of: collectionView)
【讨论】:
以上是关于检测同一控制器中的 TableView 和 CollectionView 滚动的主要内容,如果未能解决你的问题,请参考以下文章
在 TableView 中检测 UIGestureRecognizer 的出处单元
如何检测对 UITableView 的触摸,到目前为止似乎没有任何效果?
如何根据javafx中的Action为同一个tableview提供一个Contextmenu?