在ViewController中滚动多个CollectionView的冲突

时间:2017-05-15 19:19:49

标签: ios swift uicollectionview

我的UICollectionView中有两个collectionViewA collectionViewBViewController,当按下按钮时,collectionViewB显示为子视图,问题我有现在是当我滚动collectionViewBcollectionViewA滚动时,有没有办法只滚动活动collectionView而不影响第二个。?

extension TrendListVC: UICollectionViewDelegate,
        UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if collectionView == self.collectionViewForSubView{
            return count
        }
        return 60
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if collectionView == self.collectionViewForSubView{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ModalViewCell

            //let setting = settings[indexPath.item]
            //cell.setting = setting
            return cell
        }else{

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as! CollectionCell

            cell.itemNameLabel.text = "Name".uppercased()


            return cell
        }

    }

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {


        var reusableView : CollectionHeader? = nil

        if collectionView == self.collectionView{
            if (kind == UICollectionElementKindSectionHeader) {
                let head = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell", for: indexPath) as! CollectionHeader
                head.trendListVc = self
                head.headerHeightConstraint = headerHeightConstraint
                reusableView = head
            }
        }

        return reusableView!
    }


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        if collectionView == self.collectionViewForSubView{

            if count % 2 != 0 && indexPath.item == count - 1{
                let paddingSpace = sectionInsets.left * (itemsPerRowForSubView + 1)
                let availableWidth = collectionViewForSubView.frame.width - paddingSpace
                return CGSize(width: availableWidth + 23, height: cellHeight)
            }else{

                let paddingSpace = sectionInsets.left * (itemsPerRowForSubView + 1)
                let availableWidth = collectionViewForSubView.frame.width - paddingSpace
                let widthPerItem = availableWidth / itemsPerRowForSubView

                return CGSize(width: widthPerItem, height: cellHeight)
            }
        }else{
            let paddingSpace = sectionInsets.left * (2 + 1)
            let availableWidth = view.frame.width - paddingSpace
            let widthPerItem = availableWidth / itemsPerRow

            return CGSize(width: widthPerItem + 4, height: widthPerItem)
        }
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

        if collectionView == self.collectionView{
            return CGSize(width: view.frame.width, height: 50)
        }else{
            return CGSize(width: 0, height: 0)
        }
    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if collectionView == self.collectionView{
            showControllerForSetting(setting: "Name")
        }else if collectionView == self.collectionViewForSubView{
            print("Some thing")
        }
    }

}

1 个答案:

答案 0 :(得分:0)

这很可能是因为您将ViewController用作两个集合视图的委托和数据源。您需要为每个集合视图创建单独的委托类。