what is the equivalent for _:layout:sizeForItemAt: in compositional layout ?

for example. I want to create 100 items (rows). each item(row) will have 100% width and height for ((i+1)*10) :

item-row 0 = height 10.
item-row 1 = height 20.
item-row 2 = height 30

this is just for testing purpose, but i will have real fixed heights before loading the collection view.

with flow layout I used _:layout:sizeForItemAt: and got set the size from my model-entities

how can i achieve this with compositional layout ? I don't want to use Self size cell or/and auto layout. just continue to set the height my self, but enjoy the other benefits of the compositional layout.

i will use it on appkit and uikit.

thanks.

Replies

short answer : NSCollectionLayoutGroup customGroupWithLayoutSize

then, inside the block we need to return -ALL- items frames any time the system ask for us.

next code will create custom group, centered, each row is height for ((i+1)*10) // just for testing and learning

    NSCollectionLayoutGroup * group = [NSCollectionLayoutGroup customGroupWithLayoutSize:groupSize itemProvider:^NSArray<NSCollectionLayoutGroupCustomItem *> * _Nonnull(id<NSCollectionLayoutEnvironment>  _Nonnull layoutEnvironment) {

       

        

        NSMutableArray<NSCollectionLayoutGroupCustomItem*>* customItems = [@[]mutableCopy];

        

        CGFloat x=0, y=0;

        CGFloat height;

        CGFloat horizontal_edge_space = ceil((layoutEnvironment.container.contentSize.width-300) / 2);

        

        for(int i =0; i<100 ; i++)

        {

            height = (i+1) * 10;

            NSCollectionLayoutGroupCustomItem * customItem =[NSCollectionLayoutGroupCustomItem customItemWithFrame:CGRectMake(horizontal_edge_space,y,300,height)];

            [customItems addObject:customItem];

            y=y+height;

        }

        return [customItems copy];

    }];