如果同时在一个画面实作两个UICollectionView

陳尹
  • 348

以下面的程式为例,这是一个可正常执行的的collectionView

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
    var m_collectionView:UICollectionView!
    var m_layout:UICollectionViewFlowLayout!
    func refreshWithFrame(frame:CGRect){
        self.view.frame = frame
        self.view.backgroundColor = UIColor.lightGrayColor()
        m_layout = UICollectionViewFlowLayout()
        m_layout.minimumLineSpacing = 1.5
        m_layout.minimumInteritemSpacing = 1
        m_collectionView = UICollectionView(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height), collectionViewLayout: m_layout)
        m_collectionView.backgroundColor = UIColor.whiteColor()
        m_collectionView.delegate = self
        m_collectionView.dataSource = self
        m_collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "TESTCell")
        self.view.addSubview(m_collectionView)
    
    }
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }
    
    /*func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        var reuseView:UICollectionReusableView!
        reuseView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "TestHeader", forIndexPath: indexPath)
        reuseView.backgroundColor = UIColor.blackColor()
        return reuseView
    } */
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        var cell = collectionView.dequeueReusableCellWithReuseIdentifier("TESTCell", forIndexPath: indexPath)
                    cell.backgroundColor = UIColor.lightGrayColor()
                return cell
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        //返回每个cell的大小
        return CGSize(width: self.view.frame.size.width / 3 - 10, height:100)
    }
    
        
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
        return UIEdgeInsets(top:0, left: 0, bottom:5, right: 0)
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        //Cell点击事件
    }
    
}

下面有几个一针对ColloectionView一定要的func

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
    return UIEdgeInsets(top:0, left: 0, bottom:5, right: 0)
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    //Cell点击事件
}

我现在 需要2个,A=UIColloectionView(), 跟 B = UICollectionView()

那有关 memberofsectionCollectionview numberOfItemsInSection didSelectItemAtIndexPath
等几个都是不同的,那我该如何去实作两个collectionView呢??
请教各位先进....

回复
阅读 3.4k
1 个回答
✓ 已被采纳

举例说明吧,简单的实现的话,可以类似下面这样:

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    if collectionView == A {
        return 1
    } else if collectionView == B {
       return 2 
    }
    return 0
}

复杂的话,也可能需要把 A,B 两个 collection view 的 datasource 和 delegate 都设置为单独的类,不一定都要设置成当前的 view controller 的。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏