iOS 使用UICollectionView实现瀑布流

来源:互联网 发布:mac黑屏问号文件夹闪烁 编辑:程序博客网 时间:2024/05/16 12:28

#pragma mark----UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    returnself.itemData.count;

}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    //2列的瀑布流

    MatchedCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:@"MatchedCell"forIndexPath:indexPath];

    //移除cell

//    for (id subViews in cell.contentView.subviews) {

//        [subViews removeFromSuperview];

//    }

    //当前列数

    NSInteger remaineder = indexPath.row %2;

    //当前行数

    NSInteger currentRow = indexPath.row /2;

    

    //取出单元格的高度

    CGFloat currentHeight = [self.hArr[indexPath.row]floatValue] ;

    

    //算出cellx起始坐标

    CGFloat positionX = (kScreenWidth /2 -8) * remaineder +5 * (remaineder +1);

    //算出celly起始坐标

    CGFloat positionY = (currentRow +1) *5;

    

    for (int i =0; i < currentRow; i++) {

        //遍历出此单元格之前所有的单元格下标,然后将其对应的高度相加,求出当前的高度

        NSInteger position = remaineder + i *2;

        positionY += [self.hArr[position]floatValue] ;

    }

    //从新定义单元格的位置与高度

    cell.frame =CGRectMake(positionX,_matchedHeader.bottom -75 + positionY, kScreenWidth /2 -8, currentHeight);

//    cell.backgroundColor = [UIColor redColor];

    cell.itemModel =self.itemData[indexPath.row];

    return cell;

}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

    returnCGSizeMake(kScreenWidth /2 - 8, [self.hArr[indexPath.row]floatValue]);

}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

    returnUIEdgeInsetsMake(0,0, -_edgHeight,0);

}

0 0
原创粉丝点击