0

In my iPhone app,I want to use custom lay out in UICollectionview.There will different heights for Collection view cells.I want to set a lay out like that. I tried many things and at last,made that height adjustment.(I am getting height for each object from server)

But the layout is not in a proper way.There are lots of spaces in between the items. How can I achieve a proper layout.

Now I am getting a view like this enter image description here

What I want is -

enter image description here

Here is my code -

-(CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout heightForItemAtIndexPath:(NSIndexPath*)indexPath
{
  return 60;
}


 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
   return array.count;   // getting from server
   }


 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

   CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];

   cell.imageBG.image = [imagearray objectAtIndex:indexPath.item];


   return cell;
   }




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


    CGSize retval;


  // I'm taking height of each images into HeightArray

retval =  CGSizeMake(100, [[HeightArray objectAtIndex:indexPath.item]floatValue ]+50);


return retval;
}
user2115266
  • 137
  • 1
  • 12
  • 1
    What collection view layout are you using? Have you written your own `UICollectionViewLayout` subclass, or are you using one of the existing ones? – Caleb May 20 '13 at 15:31
  • How can I use that,I am not awaring of that. Please help me.Would love to know how to use the existing class if there any. – user2115266 May 20 '13 at 15:38

1 Answers1

1

From your screenshots it seems you are trying to use standard UICollectionViewFlowLayout (or its subclass). Unfortunately its limitation is that each row will always occupy the same height - it will be the same as in highest cell in that row, so you it is not suitable for your task and you have to implement your own layout.

Check UICollectionViewWaterfallLayout class which, although has its limitations (supports only 1 section, no decoration views) provides layout you need and at least can be a good starting point if you won't be able to use it as it is now.

Vladimir
  • 170,431
  • 36
  • 387
  • 313