ios - 一个页面中有个两个 UICollectionView ,那么应当如何使用Delegate与DataSource呢
高洛峰
高洛峰 2017-04-17 17:58:20
0
3
602

例如:

@IBOutlet weak var Collection01: UICollectionView!
@IBOutlet weak var Collection02: UICollectionView!

那么如何分别为他们定义Delegate与DataSource的方法呢,例如下面这个返回单元格数量的方法

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

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
洪涛

Use two classes as Delegate and DataSource through Extension, and you can also give a Tag to judge

巴扎黑

Write two classes specifically to be used as dataSource. In fact, there are many benefits to extracting the datasource from the controller. . .

迷茫

@Taku The first half of the sentence is the correct solution. However, the Tag judgment in the second half of the sentence is not good and the efficiency is low.
Let me tell you a simple implementation:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView == Collection01 {
        return 1
    } else if collectionView == Collection02 {
        return 2
    }
    return 0
}

PS: The first letter of the variable name should be lowercase.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template