How to make collectionview cell come close to another cell in swift

Viewed 202

I am using collectionview in tableview cell and showing collectionview height according to number of cells from JSON response according to this answer

the code:

class ViewProposalTableVIewCell: UITableViewCell, UICollectionViewDelegate,UICollectionViewDataSource {


@IBOutlet weak var attCollHeight: NSLayoutConstraint!
@IBOutlet weak var attetchmentsCollectionview: UICollectionView!

override func awakeFromNib() {
    super.awakeFromNib()

    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical
    let width = UIScreen.main.bounds.width/2.5
    let height = CGFloat(40)
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    layout.itemSize = CGSize(width: width, height: height)
    self.attetchmentsCollectionview.collectionViewLayout = layout
}
}

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "ViewProposalTableVIewCell", for: indexPath) as! ViewProposalTableVIewCell

let bidData = viewproposalData?.result?.bids?[indexPath.row]
cell.propAmt.text = "$\(bidData?.amount ?? "")"

cell.frame = tableView.bounds
cell.layoutIfNeeded()
cell.attetchmentsCollectionview.reloadData()

cell.attCollHeight.constant = cell.attetchmentsCollectionview.collectionViewLayout.collectionViewContentSize.height;

cell.attetchmentsCollectionview.reloadData()
return cell
}

then the 0/p: but i don't want the gap between the cells how to make both cells come close

enter image description here

EDIT: according to below answer and removed code in awakeFromNib then the o/p

enter image description here where am i wrong

2 Answers

Set this settings for collectionview from storybord

enter image description here enter image description here

Add this in code

extension homeVC: UICollectionViewDelegateFlowLayout,
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let noOfCellsInRow = 2
        let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
        let totalSpace = flowLayout.sectionInset.left
            + flowLayout.sectionInset.right
            + (flowLayout.minimumInteritemSpacing * CGFloat(noOfCellsInRow - 1))
        
        let size = Int((collectionView.bounds.width - 5) / CGFloat(noOfCellsInRow))
        
        return CGSize(width: size, height: 20)
    }
}

You need to set insets

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    let cellWidth = 50
    let noOfCellsInRow = 2
    let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
    let space = flowLayout.minimumInteritemSpacing
    let totalCellWidth = cellWidth * noOfCellsInRow
    
    let leftInset = (collectionView.frame.width - (CGFloat(totalCellWidth) + space)) / 2
    let rightInset = leftInset

    return UIEdgeInsets(top: 0, left: leftInset, bottom: 0, right: rightInset)
}
Related