Nikita Patil
Nikita Patil

Reputation: 724

CollectionView is not displaying vertically as expected

I am trying to implement following screen.

Expected Output

But in my code, it is displaying following way

My Output

following code I wrote,

    import UIKit

class LunchViewController: UIViewController {

    @IBOutlet weak var mainCollectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }


}

extension LunchViewController:UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate {


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 412, height: 180)
    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       // return self.myLibraryArray.count
        return 8
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        // let item = self.myLibraryArray[indexPath.row]

        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RestaurantCollectionViewCell", for: indexPath) as? RestaurantCollectionViewCell else {
            return UICollectionViewCell()
        }
        //cell.fillDetails(item)
        return cell

    }


}

Here is the complete project link

Can any one tell me, what is the issue? or what I have to write the code to get expected output?

Update:

some white Seperator is showing

Upvotes: 1

Views: 452

Answers (2)

Saifan Nadaf
Saifan Nadaf

Reputation: 1775

Select collectionView and Change Estimate Size to none. In storyboard. if you dont want space between two cells then set 0 to for lines in Min spacing

enter image description here

And i have given width as screen width

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: UIScreen.main.bounds.size.width, height: 180)
}

Output :-

enter image description here

Upvotes: 3

Frankenstein
Frankenstein

Reputation: 16341

Change sizeForItem method to this:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.frame.size.width, height: 180)
}

Also, make sure the mainCollectionView is constrained to the leading and trailing edges in the storyboard.

Upvotes: 2

Related Questions