0

在我的 iPhone 应用程序中,我想在 UICollectionview 中使用自定义布局。集合视图单元格会有不同的高度。我想设置这样的布局。我尝试了很多东西,最后进行了高度调整。(我从服务器获取每个对象的高度)

但是布局不正确。项目之间有很多空格。我怎样才能实现正确的布局。

现在我得到这样的视图 在此处输入图像描述

我想要的是——

在此处输入图像描述

这是我的代码 -

-(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;
}
4

1 回答 1

1

从您的屏幕截图中,您似乎正在尝试使用标准 UICollectionViewFlowLayout (或其子类)。不幸的是,它的限制是每行将始终占据相同的高度 - 它与该行中最高单元格的高度相同,因此它不适合您的任务,您必须实现自己的布局。

检查UICollectionViewWaterfallLayout类,尽管它有其局限性(仅支持 1 个部分,没有装饰视图)提供了您需要的布局,并且如果您不能像现在这样使用它,至少可以作为一个很好的起点。

于 2013-05-20T16:08:13.253 回答