【发布时间】: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