首页 技术 正文
技术 2022年11月15日
0 收藏 473 点赞 3,905 浏览 5200 个字

一、UICollectionView集合视图         其继承自UIScrollView。        UICollectionView类是iOS6新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView类。1、需要遵循的协议:1)UICollectionViewDataSource,2)UICollectionViewDelegate,3)UICollectionViewDelegateFlowLayout2、创建collection:UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayoutalloc] init];   
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];//初始化,并设置布局方式
    collection.backgroundColor = [UIColor whiteColor];
   
    [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@”cell”];//注册UICollectionViewCell,这是固定格式,也是必须要实现的
   
    [collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@”headerView”];//注册头/尾视图,视图类型必须为UICollectionReusableView或者其子类,kind设置为UICollectionElementKindSectionHeader或者UICollectionElementKindSectionFooter,最后设置标识
   
    collection.delegate = self;
    collection.dataSource = self;    [self.view addSubview:collection];3、需要实现的方法:1)-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{    //设置组数,不写该方法默认是一组
    return 4;
}

2)-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 9;
}

3)-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
   
    static NSString *identifier = @”cell”;//注意,此处的identifier要与注册cell时使用的标识保持一致
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
   
    cell.backgroundColor = [UIColor grayColor];
    cell.layer.borderWidth = 0.5;
    cell.layer.borderColor = [UIColor whiteColor].CGColor;
    return cell;
   
}

//设置头视图的尺寸,如果想要使用头视图,则必须实现该方法
4)-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(300, 30);
}

5)-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    //根据类型以及标识获取注册过的头视图,注意重用机制导致的bug
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@”headerView” forIndexPath:indexPath];
    headerView.backgroundColor = [UIColor brownColor];
   
    for (UIView *view in headerView.subviews) {
        [view removeFromSuperview];
    }
   
    UILabel *label = [[UILabel alloc] initWithFrame:headerView.bounds];
    label.text = [NSString stringWithFormat:@”%zi组的头视图”,indexPath.section];
    [headerView addSubview:label];
    label.textColor = [UIColor whiteColor];
   
    return headerView;
} 6)-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@”%zi组,%zi行”,indexPath.section,indexPath.item);
   
}

7)-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    //设置item尺寸
    return CGSizeMake(self.view.frame.size.width/3.0, 100);
}
8)-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    //设置组距离上向左右的间距
    return UIEdgeInsetsMake(0,0,0,0);
}

9)-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    //两个item的列间距
    return 0;
}

10)-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    //如果一组中有多行item,设置行间距
    return 0;}  二、自定义集合视图1、遵循的协议1)UICollectionViewDataSource,2)UICollectionViewDelegate,3)UICollectionViewDelegateFlowLayout 2、创建定义一个全局变量:
UICollectionView *_collectionView; _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
   
    [_collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@”cell”];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];    [self.view addSubview:_collectionView]; 3、实现的方法 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 10;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @”cell”;
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@”1.jpg”];
   
    //自定义选中背景
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor grayColor];
   
    cell.selectedBackgroundView = view;
   
    return cell;} 4、自定义一个继承自
CollectionViewCell的类为
MyCollectionViewCell1)声明一个UIImageView属性@property (nonatomic, strong) UIImageView *imageView;2)实现方法-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
       
        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        [self addSubview:_imageView];
       
    }
    return self;}   

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,360
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,852