具有动态高度的UICollectionView不会在单元格之间获得相同的空间 [英] UICollectionView with dynamic height not getting same space between cell
问题描述
我遇到类似问题中发现了这个问题还有。
我已经得到了解决这类问题的一般解决方案:
- (void)viewDidLoad
{
[super viewDidLoad];
[cView registerNib:[UINib nibWithNibName:@CViewCellbundle:nil] forCellWithReuseIdentifier:kCellID];
CViewFlowLayout * fl = [[CViewFlowLayout alloc] init];
fl.minimumInteritemSpacing = 10;
fl.scrollDirection = UICollectionViewScrollDirectionVertical;
self.cView.collectionViewLayout = fl;
NSString * strJson =返回JSON STRING的我的功能;
SBJSON * parser = [[SBJSON alloc] init];
self.arrMain = [[NSMutableArray alloc] init];
self.arrMain = [parser objectWithString:strJson error:nil];
for(int i = 0; i< [self.arrMain count]; i ++){
NSDictionary * dic = [self.arrMain objectAtIndex:i];
[self setTags:[[UIView alloc] init] selDictionary:dic]; //这个函数生成高度并将其保存到字典
}
[cView reloadData];
}
然后,更新代码在CViewFlowLayout.m中,它是 UICollectionViewFlowLayout
的子类。
以下是代码:
#define numColumns 3
@implementation CViewFlowLayout
@synthesize numOfColumnsToDisplay;
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray * attributesToReturn = [super layoutAttributesForElementsInRect:rect];
for(UICollectionViewLayoutAttributes * attributesToReturn中的属性)
{
if(nil == attributes.representedElementKind)
{
NSIndexPath * indexPath = attributes.indexPath;
attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath] .frame;
}
}
返回attributesToReturn;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes * currentItemAttributes = [super layoutAttributesForItemAtIndexPath:indexPath];
if(indexPath.item< numColumns){
CGRect f = currentItemAttributes.frame;
f.origin.y = 0;
currentItemAttributes.frame = f;
返回currentItemAttributes;
}
NSIndexPath * ipPrev = [NSIndexPath indexPathForItem:indexPath.item-numColumns inSection:indexPath.section];
CGRect fPrev = [self layoutAttributesForItemAtIndexPath:ipPrev] .frame;
CGFloat YPointNew = fPrev.origin.y + fPrev.size.height + 10;
CGRect f = currentItemAttributes.frame;
f.origin.y = YPointNew;
currentItemAttributes.frame = f;
返回currentItemAttributes;
}
此解决方案仅适用于垂直滚动。如果要显示更多或更少的列,只需更改 numColumns
。
I am having similar problem like this
I am generating height of view at run time.Here is my code
@interface CollectionViewController ()
{
NSMutableArray *arrMain;
}
@property(nonatomic,strong) NSMutableArray *arrMain;
@end
@implementation CollectionViewController
@synthesize arrMain,
- (void)viewDidLoad
{
[super viewDidLoad];
[cView registerNib:[UINib nibWithNibName:@"CViewCell" bundle:nil] forCellWithReuseIdentifier:kCellID];
CViewFlowLayout *fl = [[CViewFlowLayout alloc] init];
self.cView.collectionViewLayout = fl;
NSString *strJson = MY FUNCTION WHICH RETURNS JSON STRING;
SBJSON *parser = [[SBJSON alloc] init];
self.arrMain = [[NSMutableArray alloc] init];
self.arrMain = [parser objectWithString:strJson error:nil];
for (int i=0; i<[self.arrMain count]; i++) {
NSDictionary *dic = [self.arrMain objectAtIndex:i];
[self setTags:[[UIView alloc] init] selDictionary:dic]; // This function generates height and save it to the dictionary
}
[cView reloadData];
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
return [self.arrMain count];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dic = [self.arrMain objectAtIndex:indexPath.row];
return CGSizeMake(236,40+[[dic valueForKey:@"TAGS_HEIGHT"] intValue]);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
CViewCell *cell =(CViewCell *) [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
NSDictionary *dic = [self.arrMain objectAtIndex:indexPath.row];
if ([cell viewWithTag:11]){
[[cell viewWithTag:11] release];
[[cell viewWithTag:11] removeFromSuperview];
}
UIView *viewTags = [[UIView alloc] init];
[viewTags setTag:11];
[viewTags setBackgroundColor:[UIColor lightGrayColor]];
[self setTags:viewTags selDictionary:dic];
[viewTags setFrame:CGRectMake(5, 10, CONTENT_WIDTH, [[dic valueForKey:@"TAGS_HEIGHT"] floatValue])];
[cell.contentView addSubview:viewTags];
return cell;
}
I had tried above link solution but it is not working for me. Here is an image of my output.
I need the spacing to be same. Is anyone having solution for this issue ?
Is this bug of UICOllectionView ? Because I found this issue in this article also.
I've got the general solution for this kind of problem:
- (void)viewDidLoad
{
[super viewDidLoad];
[cView registerNib:[UINib nibWithNibName:@"CViewCell" bundle:nil] forCellWithReuseIdentifier:kCellID];
CViewFlowLayout *fl = [[CViewFlowLayout alloc] init];
fl.minimumInteritemSpacing = 10;
fl.scrollDirection = UICollectionViewScrollDirectionVertical;
self.cView.collectionViewLayout = fl;
NSString *strJson = MY FUNCTION WHICH RETURNS JSON STRING;
SBJSON *parser = [[SBJSON alloc] init];
self.arrMain = [[NSMutableArray alloc] init];
self.arrMain = [parser objectWithString:strJson error:nil];
for (int i=0; i<[self.arrMain count]; i++) {
NSDictionary *dic = [self.arrMain objectAtIndex:i];
[self setTags:[[UIView alloc] init] selDictionary:dic]; // This function generates height and save it to the dictionary
}
[cView reloadData];
}
Then, update the code in CViewFlowLayout.m, which is a subclass of UICollectionViewFlowLayout
.
Here is the code:
#define numColumns 3
@implementation CViewFlowLayout
@synthesize numOfColumnsToDisplay;
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray* attributesToReturn = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes* attributes in attributesToReturn)
{
if (nil == attributes.representedElementKind)
{
NSIndexPath* indexPath = attributes.indexPath;
attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame;
}
}
return attributesToReturn;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes* currentItemAttributes = [super layoutAttributesForItemAtIndexPath:indexPath];
if (indexPath.item < numColumns){
CGRect f = currentItemAttributes.frame;
f.origin.y = 0;
currentItemAttributes.frame = f;
return currentItemAttributes;
}
NSIndexPath* ipPrev = [NSIndexPath indexPathForItem:indexPath.item-numColumns inSection:indexPath.section];
CGRect fPrev = [self layoutAttributesForItemAtIndexPath:ipPrev].frame;
CGFloat YPointNew = fPrev.origin.y + fPrev.size.height + 10;
CGRect f = currentItemAttributes.frame;
f.origin.y = YPointNew;
currentItemAttributes.frame = f;
return currentItemAttributes;
}
This solution will work for vertical scrolling only. If you want to display more or less columns, just change the numColumns
.
这篇关于具有动态高度的UICollectionView不会在单元格之间获得相同的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!