Как сделать 2 ячейки в виде коллекции строк - PullRequest
0 голосов
/ 03 августа 2020

Я пытаюсь получить 2 изображения подряд, как на скриншоте.

What result I want

but this is my real result

мой реальный результат

вот мое расширение ViewController:

extension ViewController : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count // images is an array of my UIImages`s
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellImage", for: indexPath) as! imageCollectionViewCell
        cell.imageView.image = images[indexPath.item]
        cell.imageView.contentMode = .scaleAspectFill
        cell.imageView.clipsToBounds = true
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let frameCV = collectionView.frame//размер collectionView
        
        let cellWidth = frameCV.width / CGFloat(countOfCells) // countOfCells == 2
        let cellHeight = cellWidth
        
        return CGSize(width: cellWidth, height: cellHeight)

    }
}

1 Ответ

1 голос
/ 03 августа 2020

Вам нужно добавить отступ

extension ViewController: UICollectionViewDelegateFlowLayout {
    
 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        let leftAndRightPaddings: CGFloat = 45.0
        let numberOfItemsPerRow: CGFloat = 2.0
    
        let width = (collectionView.frame.width-leftAndRightPaddings)/numberOfItemsPerRow
        return CGSize(width: width, height: width) // You can change width and height here as pr your requirement
    
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...