skip to Main Content

Hello fellow overflowers so I set up a collectionView and configure it but when I run the simulator I get a lot of cells just mixed with each other like in the first picture.

I have added the identifier at the storyboards cell also created the class for the cell but I can’t seem to know what the problem might be since it never happened.

How I want my cell to be I will show on the second picture.

first picture

second picture

the code:

extension dashboardViewController: UICollectionViewDelegate, UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
    return 15
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dashboardCollectionViewCell", for: indexPath) as? dashboardCollectionViewCell else {return UICollectionViewCell()}
    
    return cell
  }
}

2

Answers


  1. Seems that your collection view cell’s size has not correct set.
    If your are using UICollectionViewFlowLayout, maybe you can check the layout object’s itemSize property.

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    ...
    
    // Set a suitable CGSize for the `itemSize` property
    // In this example I set it to 100pt * 50pt
    layout.itemSize = CGSizeMake(100, 50);
    
    ...
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    
    Login or Signup to reply.
  2. By the image provided, it seems you haven’t set the item size of the cell
    in horizontal layout as well as the minimum spacing between each cell. You can do that (set item size) by calling

    func collectionView(
      _ collectionView: UICollectionView,
      layout collectionViewLayout: UICollectionViewLayout,
      sizeForItemAt indexPath: IndexPath
    ) -> CGSize {}
    

    this method from UICollectionViewDelegateFlowLayout.
    You can also do this using storyboard.
    Size Inspector

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search