例如:
@IBOutlet weak var Collection01: UICollectionView!
@IBOutlet weak var Collection02: UICollectionView!
那么如何分别为他们定义Delegate与DataSource的方法呢,例如下面这个返回单元格数量的方法
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
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
巴扎黑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. . .
迷茫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.