如何确定当前在滚动视图中显示哪个子视图?

时间:2017-07-13 05:44:58

标签: swift

我是一个快速的初学者。我有以下视图设置,如下面的屏幕截图所示。

它由顶部的CollectionView和两个单元格(已完成并打开)和一个UISCrollsview组成,其下方有两个TableView子视图。

我希望能够滚动到下一个视图(打开Tableview列表)并通过更改顶部的选定单元格(OPEN)来指示。反之亦然,如果我点击OPEN,我希望下面的ScrollView自动滚动到正确的子视图。

所以我的问题是:

  1. 如何确定正在滚动显示的当前子视图?
  2. 如何根据顶部集合视图中选定的单元格滚动到正确的子视图?
  3. enter image description here

    以下是控制器类的代码:

    class OrdersViewController: UIViewController {
    
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var ordersListSV: UIScrollView!
    
    var orderStatus: [String] = ["Completed", "Open"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.navigationItem.title = "ORDERS"
    
        //creating the first view to show
        let completedOrderView: CompletedOrdersTableViewController = CompletedOrdersTableViewController(nibName: "CompletedOrdersTableViewController", bundle: nil)
        //showing the first view
        self.addChildViewController(completedOrderView)
        self.ordersListSV.addSubview(completedOrderView.view)
        completedOrderView.didMove(toParentViewController: self)
    
        //creating the second view to show
        let openOrderView: OpenOrdersTableViewController = OpenOrdersTableViewController(nibName: "OpenOrdersTableViewController", bundle: nil)
        //showing the second view
        self.addChildViewController(openOrderView)
        self.ordersListSV.addSubview(openOrderView.view)
        openOrderView.didMove(toParentViewController: self)
    
        //Adding the second view to the frame
        var OpenOrderViewFrame: CGRect = openOrderView.view.frame
        OpenOrderViewFrame.origin.x = self.view.frame.width
        openOrderView.view.frame = OpenOrderViewFrame
    
        //Allowing it to scroll over to the second view
        self.ordersListSV.contentSize = CGSize(width: ordersListSV.bounds.width * 2, height: ordersListSV.bounds.height)
    
    }
    

    }

    扩展OrdersViewController:UICollectionViewDelegate,UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return orderStatus.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "OrderStatusCollectionViewCell", for: indexPath) as! OrderStatusCollectionViewCell
    
        cell.lbOrderState.text = orderStatus[indexPath.row].uppercased()
    
        return cell
    
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
        print(indexPath.item)
    
    
    }
    
    override func viewDidAppear(_ animated: Bool) {
    
        super.viewDidDisappear(false)
    
        let indexPathForFirstRow = NSIndexPath.init(row: 0, section: 0)
    
        collectionView?.selectItem(at: indexPathForFirstRow as IndexPath, animated: false, scrollPosition: .top)
    
    }
    

    }

0 个答案:

没有答案