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

  • 相关阅读:
    IBatis.Net使用总结(三)-- IBatis实现分页返回数据和总数
    IBatis.Net使用总结(二)-- IBatis返回DataTable/DataSet(网上例子的集合)
    IBatis.Net使用总结(一)-- IBatis解决SQL注入(#与$的区别)
    ibatis.net 中SqlMaps的xml文件的例子
    ibatis.net MVC 单元测试 错误解决方法
    TreeView checkbox 全选
    Win下Eclipse提交Hadoop程序出错:org.apache.hadoop.security.AccessControlException: Permission denied: user=D
    端口被占用怎么办?如何查看win7电脑端口是否被占用
    Python 3.7运行别人的Python 3.8项目,报Cannot run program "D:pythonpython.exe"解决办法
    Python学习系列之常见错误TypeError,try...except...finally等异常处理机制(二十二)
  • 原文地址:https://www.cnblogs.com/adodo/p/5237420.html
Copyright © 2011-2022 走看看