Iphone UICollectionView自定义布局-单元格排列不正确
在我的iPhone应用程序中,我想在UICollectionview中使用自定义布局。集合视图单元格将有不同的高度。我想设置这样的布局。 我尝试了很多方法,最后进行了高度调整。(我从服务器获取每个对象的高度) 但是布局不恰当。项目之间有很多空间。 如何实现正确的布局 现在我看到的是这样的景象 我想要的是- 这是我的密码-Iphone UICollectionView自定义布局-单元格排列不正确,iphone,ios,uicollectionview,uicollectionviewlayout,Iphone,Ios,Uicollectionview,Uicollectionviewlayout,在我的iPhone应用程序中,我想在UICollectionview中使用自定义布局。集合视图单元格将有不同的高度。我想设置这样的布局。 我尝试了很多方法,最后进行了高度调整。(我从服务器获取每个对象的高度) 但是布局不恰当。项目之间有很多空间。 如何实现正确的布局 现在我看到的是这样的景象 我想要的是- 这是我的密码- -(CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLay
-(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;
}
从您的屏幕截图来看,您似乎正在尝试使用标准的UICollectionViewFlowLayout(或其子类)。不幸的是,它的局限性是每一行始终占据相同的高度-它将与该行中最高的单元格中的高度相同,因此您不适合您的任务,您必须实现自己的布局
检查类,该类虽然有其局限性(仅支持1个剖面,不支持装饰视图),但提供了您需要的布局,如果您不能像现在这样使用它,至少可以作为一个良好的起点。您使用的是什么集合视图布局?您是否编写了自己的
UICollectionViewLayout
子类,或者您正在使用现有的子类之一?我如何使用它,我还没有意识到这一点。请帮助我。如果有的话,我想知道如何使用现有的类。谢谢vladimir。。希望我能在这方面提出疑问,如果有:)