堆栈UICollectionView部分

时间:2016-04-20 14:47:52

标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout

我有问题。我使用UICollectionView显示两个(它们将更多)图像数组,每个数组都有自己的部分。我希望实现以下目标:

-----------------------
Section 1:   Elements in array no. 1
-----------------------
Section 2:   Elements in array no. 2
-----------------------

但截至目前,我得到了类似

的内容
-----------------------
Section 1:  Elements in array no. 1 | Elements in array no. 2
-----------------------

如何告诉UICollectionView堆叠这两个部分? 我想过把我现在的集合视图放到另一个集合视图的单元格中,但我不知道该怎么做。 另一个解决方案可能是将它们放入tableview的单元格中,但如果我可以避免这些变通方法,它会是最好的。

我不确定如何使用它,但我认为创建自定义布局可能会有效,即使我需要一些帮助。

另外,我被要求不使用任何外部库。

BTW我在Objective-C编程。

1 个答案:

答案 0 :(得分:0)

您只需实施UICollectionViewDataSource方法numberOfSectionsInCollectionView

即可制作多个部分
class ViewController: UIViewController, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

    let HEADER_IDENTIFIER = "header_identifier"
    let CELL_IDENTIFIER = "cell_identifier"
    lazy var collectionView:UICollectionView = {

        var cv = UICollectionView(frame: self.view.bounds, collectionViewLayout: self.flowLayout)
        cv.delegate = self
        cv.dataSource = self
        cv.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: self.CELL_IDENTIFIER)
        cv.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: self.HEADER_IDENTIFIER)
        cv.backgroundColor = UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1)
        return cv
    }()

    lazy var flowLayout:UICollectionViewFlowLayout = {
        var flow = UICollectionViewFlowLayout()
        flow.sectionInset = UIEdgeInsetsMake(2.0, 2.0, 2.0, 2.0)
        return flow
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(self.collectionView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: UICollectionView
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CELL_IDENTIFIER, forIndexPath: indexPath)
        cell.backgroundColor = UIColor.blueColor()
        return cell
    }

    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: HEADER_IDENTIFIER, forIndexPath: indexPath)
        header.backgroundColor = UIColor.redColor()
        return header
    }

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

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

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSize(width: 90, height: 90) // The size of one cell
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSizeMake(self.view.frame.width, 1)  // Header size
    }
}