UICollectionView with dynamic height not getting same space between cell

后端 未结 6 1807
野的像风
野的像风 2021-02-14 02:59

I am having similar problem like this

I am generating height of view at run time.Here is my code

@interface CollectionViewController ()
{
    NSMutableAr         


        
6条回答
  •  伪装坚强ぢ
    2021-02-14 03:43

    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.

提交回复
热议问题