UICollectionView的使用介绍

来源:互联网 发布:淘宝店铺点击率 编辑:程序博客网 时间:2024/06/06 05:33

UICollectionView的使用介绍
使用的时候,需要包含
UICollectionViewDataSource,
UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout这三个;

1、UICollectionView的初始化,这里cell及header采用的是xib文件

   UICollectionViewFlowLayout *flowLayout=[[[UICollectionViewFlowLayout alloc] init] autorelease];    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];    self.collectionView=[[[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout] autorelease];    self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;    self.collectionView.dataSource=self;    self.collectionView.delegate=self;    [self.collectionView setBackgroundColor:[UIColor clearColor]];    self.collectionView.backgroundColor = [UIColor blackColor];    //注册Cell,必须要有    [self.collectionView registerNib:[UINib nibWithNibName:@"FileCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"FileCell"];        //注册HeaderView,根据需求    [self.collectionView registerNib:[UINib nibWithNibName:@"FileCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FileCollectionReusable"];    [self.view addSubview:self.collectionView];

2、UICollectionView DataSource

#pragma mark -- UICollectionViewDataSource//定义展示的UICollectionViewCell的个数-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{    return 10;}//定义展示的Section的个数-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{    return 2;}- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{    FileCollectionReusableView *reusableview = nil;    if (kind == UICollectionElementKindSectionHeader) {        reusableview =  (FileCollectionReusableView*)[collectionView dequeueReusableSupplementaryViewOfKind :kind   withReuseIdentifier:@"FileCollectionReusable"  forIndexPath:indexPath];        // header view 处理代码    }    return reusableview;}//每个UICollectionView展示的内容-(CHFileCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    static NSString * CellIdentifier = @"FileCell";    FileCollectionViewCell * cell = (FileCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];    // cell 的显示处理    return cell;}

3、UICollectionViewDelegateFlowLayout

//返回头headerView的大小-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{    CGSize size= CGSizeMake(self.view.frame.size.width, 30);    return size;}//定义每个UICollectionView 的大小- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{    CGFloat collectionCellWidth = (self.view.frame.size.width - 5 * 6)/3;    return CGSizeMake(collectionCellWidth, collectionCellWidth+30);}//定义每个UICollectionView 的 margin-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{    return UIEdgeInsetsMake(5, 5, 5, 5);}

4、UICollectionViewDelegate

//UICollectionView被选中时调用的方法-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{}//返回这个UICollectionView是否可以被选择-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{    return YES;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 网上买的燃气灶连接用软管怎么办 哈弗h3后减振器上支架坏了怎么办 汽车用收录机电机皮带断了怎么办 大便太粗把马桶堵了怎么办 大便过粗把马桶堵了怎么办 大便硬的像石头拉不出来怎么办 家里埋在地下的水管漏水怎么办 埋在墙里的水管漏水怎么办 开发商暗埋水管热熔接头漏水怎么办 钻墙把水管钻破了怎么办 钻墙时不小心把水管钻破了怎么办 孩子把同桌的铅笔盒弄坏了怎么办 水管伐坏了物业不给修怎么办 卫生间埋在地下的水管漏水怎么办 埋在地下的水管冻住了怎么办 洗脸盆下水管有眉笔掉堵了怎么办 楼上打空调眼导致墙面潮湿怎么办 阳台做防水了又贴砖还漏水怎么办 洗菜盆里的盖子吸住拔不起来怎么办 掉小区阴井盖里物业不管怎么办 卫生间下水道被头发堵了怎么办妙招 久量时尚夜光台灯不亮了怎么办 我想做水电暖工程没有资质怎么办 修手机被弄丢了没有开维修单怎么办 手机去维修被老板弄丢了怎么办 5年保修期过后房子楼顶漏水怎么办 墙内水管漏水怎么办3步骤巧维修 下面有地暖下面水管漏水怎么办 热水器接的塑料水管子漏水怎么办 电热水器烧热水时热水管漏水怎么办 户户通位置锁定模块异常1怎么办 户户通位置锁定模块异常3怎么办 逆战安全系统检测客户端异常怎么办 电视户户通位置信息模块异常怎么办 苹果手机wi-fi模块坏了怎么办 xp本地连接受限制或无连接怎么办 公司没帮二级建造师延续注册怎么办 考造价师考过了年限不够怎么办? 上海众非称重不显示数字怎么办 宝骏630防火墙隔热棉掉了怎么办 冷车启动水温报警灯亮了怎么办