zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    objectiveC中的序列化(serialize)与反序列化(deserialize)
    objectiveC 的代码文件组织
    [转载]Multicast Explained in Flash 10.1 P2P
    几种异步操作方式
    objectiveC 的内存管理之自动释放池(autorelease pool)
    用VS2010调试微软开放的部分源码
    浏览器窗口尺寸改变时的图片自动重新定位
    数据结构C#版笔记啥夫曼树(Huffman Tree)与啥夫曼编码(Huffman Encoding)
    objectiveC 的内存管理之实例分析
    objectiveC 的OOP(上)类定义、继承及方法调用
  • 原文地址:https://www.cnblogs.com/adodo/p/5237420.html
Copyright © 2011-2022 走看看