【问题标题】:Detect TableView and CollectionView scrolling in same controller检测同一控制器中的 TableView 和 CollectionView 滚动
【发布时间】:2017-04-25 13:23:24
【问题描述】:

我在控制器中有CollectionViewTableView。我通过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()
            }

        }

    }

【问题讨论】:

  • 带标签可以检测哪个组件在滚动

标签: ios swift uitableview uiscrollview uicollectionview


【解决方案1】:

由于UICollectionViewUITableView 继承自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")
    }
  }
}

或者,如果您为UITableViewUICollectionView 存储了属性,则可以对传递给scrollViewscrollView 使用相等性检查来确定哪个是哪个:

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)
            {
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多