ios – 具有动态高度的UICollectionView单元格之间的空间不会相同

ios – 具有动态高度的UICollectionView单元格之间的空间不会相同,第1张

概述我有类似的问题 like this 我在运行时生成视图的高度.这是我的代码 @interface CollectionViewController (){ NSMutableArray *arrMain;}@property(nonatomic,strong) NSMutableArray *arrMain;@end@implementation CollectionViewC 我有类似的问题 like this

我在运行时生成视图的高度.这是我的代码

@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;}

我试过上面的链接解决方案,但它不适用于我.
这是我的输出的图像.

我需要的间距是相同的.
有人有解决这个问题的人吗?

这是UICollectionVIEw的错误吗?因为我在this article也发现了这个问题.

解决方法 我有一个这样的问题的一般解决方案:
- (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];

}

然后,更新CVIEwFlowLayout.m中的代码,它是UICollectionVIEwFlowLayout的一个子类.

这是代码:

#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;}

此解决方案仅适用于垂直滚动.如果要显示更多或更少的列,只需更改numColumns.

总结

以上是内存溢出为你收集整理的ios – 具有动态高度的UICollectionView单元格之间的空间不会相同全部内容,希望文章能够帮你解决ios – 具有动态高度的UICollectionView单元格之间的空间不会相同所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1097877.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存