P
P
PavelTango2021-12-04 13:22:03
iOS
PavelTango, 2021-12-04 13:22:03

CollectionView how to create/edit cells in storyboard?

Good day! I'm trying to develop on iOS, I got stuck on one of the main design elements - collectionView, created the collectionView itself in the storyboard, several cells, edited them the way I needed, added images to them, and everything would be fine, but after launching the application, the cells do not are displayed. Having searched for a problem on the Internet, I did not find the answer I needed, everyone writes about how to create / edit cells programmatically. Actually, the question itself is, is it possible to create and fill cells without code, just in a storyboard?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladislav Resly, 2021-12-10
@PavelTango

It's impossible to fix the problem without seeing your code, but I can guess what the problem might be.
You may have set up cells in the storyboard, but at the same time, you need to tell the collection which cells to load.
That is, you arrange the elements in the storyboard as you need, and be sure to assign an identifier to the cell, for example "item", and, preferably, specify a custom class.
After that, you have a method in your CollectionViewController

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

This method will configure the cells, as we see from the method, it should return the "UICollectionViewCell" object.
In it, we need to create a cell with the identifier specified in the storyboard, and cast it to the desired class, if we created it.
For example, we set the identifier "item" and the class "CustomCell"
func collectionView(_ collectionView: UICollectionView,  cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier "item",  for: IndexPath) as! CustomCell

     // теперь cell у нас нужная нам ячейка, настройки IBOutlets и IBActions у нас в ее кастомном классе, если 
         необходимо можем тут настроить какую нибудь логику. После настройки логики, нам нужно вернуть из 
         метода эту ячейку

     return cell
}

Now the collection will configure our cells at startup by the specified ID, with the specified class. If you configured everything correctly, then you should see the necessary cells.

B
briahas, 2021-12-04
@briahas

Is it even possible to create and fill cells without code, just in a storyboard?

Of course you can.
And that's why your cells are not displayed - you need to look at the code. There are no seers here.
If you want - find me in the cart (briahas), you can share the screen, we will analyze it in real time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question