How to give the size to CollectionView cell according to image displaying in it?
Asked
Active
Viewed 3,048 times
0
-
collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath .Use this method, moreover follow this [resizing link](https://stackoverflow.com/questions/40974973/how-to-resize-the-collection-view-cells-according-to-device-screen-size) – Aditya Srivastava Aug 14 '17 at 06:49
-
I've already used it but my control is not going to that method..... – Vivek Tyagi Aug 14 '17 at 06:52
-
Check whether you have set collectionView delegate or not. – Aditya Srivastava Aug 14 '17 at 06:53
1 Answers
2
You need to confirm protocol called UICollectionViewDelegateFlowLayout
// EXTENSION FOR Controller class
extension Controller:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
// NUMBER OF SECTION IN TABLE
public func numberOfSections(in collectionView: UICollectionView) -> Int{
return 1
}
// NUMBER OF ROWS IN PARENT SECTION
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return Array.count
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
let Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ControllerCell
Cell.menuHeader.text = Array[indexPath.row]
return parentCell
}
// SIZE FOR COLLECTION VIEW CELL
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
return CGSize(width: view.frame.width, height: 150)
}

Tushar Sharma
- 2,839
- 1
- 16
- 38
-
Yes, now its going to that method and changing the size of cell. thanks a lot. – Vivek Tyagi Aug 14 '17 at 07:41
-