Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView: different size items is not calculated on reused items

I have a collection view with varied item sizes which i declare in

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout*)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    FeedItem *item = [_imagesLinkArray objectAtIndex:indexPath.row];
    return CGSizeMake(250, 200*item.sizeFactor);
}

When the cell is being reused in collectionView:cellForItemAtIndexPath: the item is being rendered with the reuesed item size and not the one specified in sizeForItemAtIndexPath:.

any ideas?

like image 539
Oren Zitoun Avatar asked Nov 10 '12 01:11

Oren Zitoun


People also ask

How does UICollectionView work?

UICollectionView makes adding custom layouts and layout transitions, like those in Photos, simple to build. You're not limited to stacks and grids because collection views are customizable. You can use them to make circle layouts, cover-flow style layouts, Pulse news style layouts and almost anything you can dream up!

What is a UICollectionView?

An object that manages an ordered collection of data items and presents them using customizable layouts.

What is Preferredlayoutattributesfitting?

Gives the cell a chance to modify the attributes provided by the layout object.

How do I add a section title in UICollectionView?

There are no section headers in the UICollectionView. So for your first task, you'll add a new section header using the search text as the section title. To display this section header, you'll use UICollectionReusableView .


2 Answers

It seems that collectionView:layout:sizeForItemAtIndexPath: is called once on layout preparation, before CollectionView is rendered.

So whenever you scroll and a cell is dequeued it is resized to the size provided by collectionView:layout:sizeForItemAtIndexPath: at preparation stage. Are you expecting this method to be called whenever a cell with certain indexPath gets visible? So that you can dynamically change cell size? seems this is not how this method can be used, it can be used to determine the size of a cell once at layout preparation stage.

In case you need the sizes to be recomputed for some reason, you can invalidate the layout using [UICollectionViewLayout invalidateLayout].

like image 83
Ali Avatar answered Oct 08 '22 00:10

Ali


A collection view seems to recompute items less often than a UITableView. You have to inform the collection view about most of the changes to the underlying model, e.g. by calling [collectionView reloadData] or [collectionView insertItemsAtIndexPaths:indexPaths].

like image 21
Marius Soutier Avatar answered Oct 07 '22 23:10

Marius Soutier