• iOS项目 瀑布流 实现


    一直对于collectionview处于半懂不懂的状态

    所以现在自己做了一个,实现效果如下

    #import "ViewController.h"

    #import "CollectionViewCell.h"

    static NSString *identifierCell = @"cellID";

    @interface ViewController ()<UICollectionViewDelegateFlowLayout>

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        self.collectionView.allowsMultipleSelection = YES;

        self.collectionView.allowsSelection = YES;

        self.collectionView.showsVerticalScrollIndicator = YES;

        self.collectionView.backgroundColor = [UIColor grayColor];

        [self registerHeaderAndFooter];

        // Do any additional setup after loading the view, typically from a nib.

    }

    -(BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath

    {

        return YES;

    }

    -(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath

    {

        CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];

        cell.selectedBtn.selected = NO;

        [cell.selectedBtn setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];

    }

    -(void)registerHeaderAndFooter{

        [self.collectionView registerNib:[UINib nibWithNibName:@"HeaderCollectionReusableView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderCollectionReusableView"];

        

        [self.collectionView registerNib:[UINib nibWithNibName:@"FooterCollectionReusableView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterCollectionReusableView"];

    }

    -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

    {

        if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {

            UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterCollectionReusableView" forIndexPath:indexPath];

            

            return footer;

        }

        

        

        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderCollectionReusableView" forIndexPath:indexPath];

        return header;

    }

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

    {

        return CGSizeMake(150, 150);

    }

    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section

    {

        return CGSizeMake(80, 80);

    }

    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

    {

        return CGSizeMake(80, 80);

    }

    //-(BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{

    //

    //    return YES;

    //

    //

    //}

    -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

    {

        

        CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];

    //    cell.selectedBtn.userInteractionEnabled = NO;

        cell.selectedBtn.selected = YES;

        [cell.selectedBtn setBackgroundImage:[UIImage imageNamed:@"2"] forState:UIControlStateSelected];

    }

    -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath

    {

        return YES;

    }

    -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

        return 5;

    }

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

    {

        return 20;

    }

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

    {

        CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifierCell forIndexPath:indexPath];

        

        return cell;

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    关键一步

    1
    2
    3
    4
    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
        CGFloat height=100+(arc4random()%100);
        return  CGSizeMake(100, height);
    }

    @end

  • 相关阅读:
    为什么puppeteer比selenium好?
    Puppeteer
    js跳出多层循环
    webpack loader- 图片处理
    webpack的loader的原理和实现
    Webpack中Loader的pitch方法
    url-loader和file-loader区别
    Vue中强制组件重新渲染的正确方法
    ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION :浏览器下载报错
    JSBridge的原理及使用
  • 原文地址:https://www.cnblogs.com/adodo/p/5237420.html
Copyright © 2020-2023  润新知