search

Home  >  Q&A  >  body text

ios - 一个页面中有个两个 UICollectionView ,那么应当如何使用Delegate与DataSource呢

例如:

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

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

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

高洛峰高洛峰2888 days ago628

reply all(3)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 18:00:20

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

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 18:00:20

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

    reply
    0
  • 迷茫

    迷茫2017-04-17 18:00:20

    @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.

    reply
    0
  • Cancelreply