【问题标题】:UICollectionView Custom Layout - Cells are not properly arrangedUICollectionView 自定义布局 - 单元格排列不正确
【发布时间】:2013-05-20 14:42:27
【问题描述】:

在我的 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;
}

【问题讨论】:

  • 您使用什么集合视图布局?您是否编写了自己的 UICollectionViewLayout 子类,或者您正在使用现有的子类之一?
  • 我如何使用它,我不知道这一点。请帮助我。如果有的话,很想知道如何使用现有的课程。

标签: iphone ios uicollectionview uicollectionviewlayout


【解决方案1】:

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

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

【讨论】:

  • 谢谢弗拉基米尔..希望我能在这方面提出疑问,如果遇到任何问题:)
猜你喜欢
  • 1970-01-01
  • 2017-05-07
  • 1970-01-01
  • 2013-03-05
  • 1970-01-01
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
相关资源
最近更新 更多