Reputation: 87
I am using collectionview in tableview cell and the hierarchy of tableview cell is like this
topview constraints top = 0, leading = 0, trailing = 0, bottom => 0
ViewX constraints top = 0 leading = 0, trailing = 0, bottom => 10
collectionviewViewX constraints top => 10 to ViewX, leading = 0, trailing = 0, bottom = 10
Attatchments collectionview constraints top = 10 to label, leading = 0, trailing = 0, bottom = 10, height = 50
Stackview constraints top = 10, leading = 0, trailing = 0, bottom = 10, height = 50
and tableview cell code: but with this code collectionview height is not changing according to cells count.. how to change collectionview height according to number of cells coming from JSON
class ProposalTableVIewCell: UITableViewCell, UICollectionViewDelegate,UICollectionViewDataSource {
@IBOutlet weak var attCollHeight: NSLayoutConstraint!
@IBOutlet weak var attetchmentsCollectionview: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 5
let width = UIScreen.main.bounds.width/2.5
let height = CGFloat(40)
layout.itemSize = CGSize(width: width, height: height)
self.attetchmentsCollectionview.collectionViewLayout = layout
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return attachArray?.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AttatchmentCollectionViewCell", for: indexPath) as! AttatchmentCollectionViewCell
var attatchBid = attachArray?[indexPath.item]
self.attCollHeight.constant = collectionView.contentSize.height
return cell
}
}
o/p there are 10 cells.. but hear all cells are not showing.. how to show all cells please do help me.. i got stuck here from long
EDIT: according to below answer o/p coming like this.. how to move cell to left side.. currently cell in the middle
override func awakeFromNib() {
super.awakeFromNib()
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
let width = UIScreen.main.bounds.width/2.1
let height = CGFloat(40)
layout.itemSize = CGSize(width: width, height: height)
self.attetchmentsCollectionview.collectionViewLayout = layout
}
Upvotes: 1
Views: 3981
Reputation: 3740
Add this to your table's cellForRow
and check if it works:
cell.frame = tableView.bounds
cell.layoutIfNeeded()
cell.attetchmentsCollectionview.reloadData()
cell.attCollHeight.constant = cell.attetchmentsCollectionview.collectionViewLayout.collectionViewContentSize.height;
EDIT:
Left aligned cell and 1 item per row, add this delegate method:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: collectionView.frame.width - flowLayout.itemSize.width)
}
Upvotes: 1