IOS UICollectionView prototype cell original property ignored

I am using a UICollectionView with two prototype cells. Prototype cells have different widths and contain various controls (image view and web view). I definitely return the correct prototype cell for the given index (all cells display the correct content), but the prototype cell size is ignored, and the size of the collection element is used instead. This is not like I manually set the size. What is the point of allowing the prototype cell size in the storyboard if the property is simply ignored when it is actually displayed?

+51
ios uicollectionview collectionview
Jan 24 '13 at 1:01
source share
7 answers

The cell size in the storyboard editor is just to help you create a cell. Since each prototype cell may be of a different size, the UICollectionView does not know what cell size should be selected for all cells. That's why you set the actual size that you want to use for your cells separately. You can do this in the designer by selecting the collection view and setting its width and height for the cell size in the size inspector in the "Collection Size" section.

Or you can override the following method and return a CGSize object that defines the size that you want to use for each cell. Using this method, you can actually have each cell of a different size:

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

Example:

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(100, 100); } 

Your view controller must be a UICollectionViewDelegateFlowLayout delegate to call this method. Therefore, be sure to add this delegate declaration to the .h file of your file, for example:

 @interface MyViewController () <UICollectionViewDelegateFlowLayout> 
+149
Jan 29 '13 at 15:44
source share

If all cells are the same size and you can set itemSize to UICollectionViewFlowLayout

Example:

 ((UICollectionViewFlowLayout *) self.collectionViewLayout).itemSize = CGSizeMake(100, 100); 
+17
Aug 11 '14 at 16:10
source share

Swift running on the latest version of iOS.

 (collectionView.collectionViewLayout as! UICollectionViewFlowLayout).itemSize = cellSize 

I don’t have enough points to answer Sam, but the key is that he is not UICollectionViewLayout, but UICollectionViewFlowLayout.

+4
Mar 02 '16 at 16:37
source share

In Swift, you can call the delegate method sizeForItemAtIndexPath to set the size for each cell.

 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { if indexPath.row == 0 { // Set the size for cell no. 0 }else if indexPath.row == 1 { // Set the size for cell no. 1 } } 

Hope this helps. Thank you

+1
May 14, '16 at 12:41
source share

If all your cells are the same size, you should simply set the UICollectionView cell size properties in the same way as the UICollectionViewCell size UICollectionViewCell .

0
Apr 24 '14 at 10:06
source share

You can set the size of the UICollectionViewCell in the properties of the UICollectionView .

From the storyboard, select Collection View in the Document Outline , and then edit the Cell Size in the Size Inspector .

And you must change the cell size type to the default value if it is executed.

0
Jan 19 '15 at 13:23
source share

Use your default delegate.,.

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGSize mElementSize; mElementSize = CGSizeMake(SCREEN_WIDTH , SCREEN_HEIGHT *0.60); return mElementSize; } 
0
Feb 17 '17 at 9:17
source share



All Articles