欢迎光临
我们一直在努力

瀑布流-collectionView

建站超值云服务器,限时71元/月

瀑布流-collectionView

项目介绍:

ImageLayout

瀑布流,网络图片瀑布流

GitHub: https://github.com/Zws-China/ImageLayout

How To Use


#import "WSLayout.h"

@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) WSLayout *wslayout;


self.wslayout = [[WSLayout alloc] init];
self.wslayout.lineNumber = 2; //列数
self.wslayout.rowSpacing = 5; //行间距
self.wslayout.lineSpacing = 5; //列间距
self.wslayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5); //内边距


// 透明时用这个属性(保证collectionView 不会被遮挡, 也不会向下移)
//self.edgesForExtendedLayout = UIRectEdgeNone;
// 不透明时用这个属性
//self.extendedLayoutIncludesOpaqueBars = YES;
self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64) collectionViewLayout:self.wslayout];

[self.collectionView registerClass:[WSCollectionCell class] forCellWithReuseIdentifier:@"collectionCell"];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
self.collectionView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:self.collectionView];


//返回每个cell的高   对应indexPath
[self.wslayout computeIndexCellHeightWithWidthBlock:^CGFloat(NSIndexPath *indexPath, CGFloat width) {

    CellModel *model = modelArray[indexPath.row];
    CGFloat oldWidth = model.imgWidth;
    CGFloat oldHeight = model.imgHeight;

    CGFloat newWidth = width;
    CGFloat newHeigth = oldHeight*newWidth / oldWidth;
    return newHeigth;
}];


#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

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

    return modelArray.count;
}

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

    WSCollectionCell *cell = (WSCollectionCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];

    cell.model = modelArray[indexPath.row];

    return cell;
}



code4app

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 瀑布流-collectionView
分享到: 更多 (0)