Ваш код, как указано, работает правильно. Я перевел его на Swift следующим образом (это полная кода моего контроллера представления:
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var cv: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
self.cv.register(UINib.init(nibName: "MyViewCell", bundle: nil), forCellWithReuseIdentifier: "cell")
self.cv.delegate = self
self.cv.dataSource = self
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 16
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = cv.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
cell.layer.borderWidth = 2
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width:self.cv.bounds.width/2, height:self.cv.bounds.height)
}
}
Вот что я вижу на симуляторе iPad:
![enter image description here](https://i.stack.imgur.com/jV3JN.png)
И это, учитывая интервал по умолчанию между 10, это именно то, что я ожидал бы увидеть.
Перо MyViewCell содержит только небольшую желтую ячейку. Я добавил границу только для ясности.