Vin*_*mar 1 uitableview uiscrollview ios uicollectionview swift
我CollectionView和TableView一个控制器.我已经制作了一个segment controlby CollectionView和实现scrollView 委托,controller如下所示: -
class MyViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate,UICollectionViewDelegateFlowLayout
Run Code Online (Sandbox Code Playgroud)
我有工具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()
}
}
}
Run Code Online (Sandbox Code Playgroud)
And*_*sek 12
既然UICollectionView和UITableView继承UIScrollView你可以使用an 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")
}
}
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您为UITableView和存储了属性,则UICollectionView可以对scrollView传递的to 使用相等性检查scrollViewDidEndDecelerating(_:)来确定哪个是:
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")
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5834 次 |
| 最近记录: |