http://postimg.org/image/6bws3catp/
正如您在图像上看到的那样,我需要一些能够根据所包含文本的长度调整单元格大小的内容。
我一直试图用UICollectionview来做这个,但是单元格总是大小相同,如果我尝试用frame.size和location参数改变大小,滚动会变得疯狂。
也许UICollectionView不是最佳选择......
非常感谢
编辑:
由于您的答案,我的单元格能够动态更改其大小,但边距不符合我的预期。
细胞的边缘总是取决于同一行的最大细胞
http://postimg.org/image/3scthf9rv/
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 6;
}
我已经实现了这个来设置6个单元格之间的边距值,但是这可能只适用于垂直集合视图中每行中有一个单元格的情况。
再次感谢您的帮助
答案 0 :(得分:0)
UICollectionView
是正确的选择。您只需要自定义流布局属性。
阅读Customizing the Flow Layout Attributes
具体而言,您需要实现Collection View Delegate方法:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
// In this method you can calculate the size of the string for each cell
UIFont *font = // font to create the text for each cell
NSString *string = // text string for each cell
CGSize size = [string sizeWithFont];
return size;
}
答案 1 :(得分:0)
您可以查看https://github.com/bryceredd/RFQuiltLayout。容貌 喜欢你可以使用的东西...
您可以覆盖以下方法并返回一个CGSize对象,该对象指定您要为每个单元格使用的大小。使用此方法,您实际上可以使每个单元格具有不同的大小:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
示例:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(100, 100);
}
您的视图控制器需要是UICollectionViewDelegateFlowLayout的委托才能调用此方法。所以不要忘记将该委托声明添加到视图控制器的.h文件中,例如:
@interface MyViewController () <UICollectionViewDelegateFlowLayout>