zoukankan      html  css  js  c++  java
  • 集合视图 代码

    #pragma mark - 设置自定义视图

    - (void)loadView

    {

        self.rootView = [[[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];

        _rootView.backgroundColor = [UIColor redColor];

        self.view = _rootView;

    }

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        // 设置数据源和代理

        self.rootView.collectionView.dataSource = self;

        self.rootView.collectionView.delegate = self;

        // collectionViewCell只能使用storyBoard(xib)或者注册使用

        [self.rootView.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];

        // 注册头区和尾区

        [self.rootView.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

        [self.rootView.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];

    }

    #pragma mark - UICollectionViewDataSource Methods

    #pragma mark 设置分区数

    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

    {

        return 3;

    }

    #pragma mark 设置每个分区的个数

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

    {

        return 9;

    }

    #pragma mark 设置每个item上显示的内容

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

    {

        // 1. 直接去重用队列中查找,如果没有的话,会自动帮我们创建

        MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];

        

        // 2. 使用

        cell.backgroundColor = [UIColor cyanColor];

        cell.label.text = [NSString stringWithFormat:@"%ld  %ld", indexPath.section, indexPath.row];

        

        // 3. 返回

        return cell;

    }

    #pragma mark 处理每个分区的头区和尾区

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

    {

        // 对于集合视图(UICollectionView)来说,头区和尾区使用的是UICollectionReusableView类,而不是UIView,设置的时候,也需要使用重用机制

        // 使用kind去判断当前是显示头区还是尾区

        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

            // 头区

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

            headerView.backgroundColor = [UIColor whiteColor];

            return headerView;

        } else {

            // 尾区

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

            footerView.backgroundColor = [UIColor redColor];

            return footerView;

        }

    }

    #pragma mark 处理item的点击

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

    {

        NSLog(@"section:%ld  row:%ld", indexPath.section, indexPath.row);

    }

    - (void)addAllViews

    {

        // 初始化集合视图

        // 1. 创建布局对象

        UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];

        

        //属性

        // 设置每一个的大小

        flowLayout.itemSize = CGSizeMake(110, 100);

        // 设置左右的最小间距

        flowLayout.minimumInteritemSpacing = 5;

        // 设置上下的最小间距

        flowLayout.minimumLineSpacing = 5;

        // 滚动方向

        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;

        // 设置头区大小

        flowLayout.headerReferenceSize = CGSizeMake(self.bounds.size.width, 20);

        // 设置尾区的大小

        flowLayout.footerReferenceSize = CGSizeMake(self.bounds.size.width, 20);

        // 设置边缘嵌入的值

        flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);

        

        // 2. 使用布局,创建集合视图,

        self.collectionView = [[[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout] autorelease];

        

        [flowLayout release];

        

        

        [self addSubview:_collectionView];

        

        

    }

    - (instancetype)initWithFrame:(CGRect)frame

    {

        self = [super initWithFrame:frame];

        if (self) {

            [self addAllViews];

            

            //自定义集合视图cell设置圆角

            self.layer.masksToBounds = YES;

            self.layer.cornerRadius = 15;

            

        }

        return self;

    }

    - (void)addAllViews

    {

        self.label = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 30)] autorelease];

        _label.backgroundColor = [UIColor yellowColor];

        _label.layer.masksToBounds = YES;

        _label.layer.cornerRadius = 10;

        

        [self.contentView addSubview:_label];

    }

  • 相关阅读:
    Codeforces 765 E. Tree Folding
    Codeforces 617 E. XOR and Favorite Number
    2017.3.4[hihocoder#1403]后缀数组一·重复旋律
    2017.2.23[hdu1814]Peaceful Commission(2-SAT)
    2017.2.18Codeforces Round #398 (Div. 2)
    2017.2.18[codevs1170]NOIP2008提高组复赛T4双栈排序
    2017.2.18[codevs3319][bzoj3670]NOI2014D2T1动物园
    2017.2.18[codevs3311][bzoj3668]NOI2014D1T1起床困难综合症
    2017.2.10 Splay总结
    2017.2.10考试总结2017冬令营
  • 原文地址:https://www.cnblogs.com/iOS-mt/p/4315255.html
Copyright © 2011-2022 走看看