iOS之UICollectionView

UICollectionView

是什么?

集合视图

视图可以对多列多行的形式来展示数据,是从UITableView演变而来的,使用上和UITableView很像

1.声明一个collectionView的属性

@property(nonatomic,strong)UICollectionView * collectionView;

2.创建布局对象(很重要)

UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
layout.itemSize=CGSizeMake(80, 80);
layout.minimumInteritemSpacing=10;
layout.minimumLineSpacing=10;
    //设置四边的间隔
layout.sectionInset=UIEdgeInsetsMake(154, 30, 154, 30);
layout.scrollDirection**重点内容**=UICollectionViewScrollDirectionHorizontal; //滑动方向 

3.初始化并添加到父视图

self.collectionView=[[UICollectionView alloc]initWithFrame:(CGRect) collectionViewLayout:layout];

//设置数据源和代理为自己
self.collectionView.delegate=self;
self.collectionViewdataSource=self;

[self.view addSubview:self.c];

[self.collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:[NSBundle mainBundle]]forCellWithReuseIdentifier:@"cell"];

//必须注册一个cell否则GG 

4.采纳协议(3个)

<UICollectionViewDataSource,UICollectionViewDelegate,UINavigationControllerDelegate>

5.实现数据源方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{  //每个Section有几项

   return  9;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//每项长什么样子
    CollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    return cell;



}

6.实现代理方法

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    //选中某行怎么处理

}

常用方法

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    //每个cell的尺寸
    return CGSizeMake(101, 169);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
  //布局相关
    return UIEdgeInsetsMake(0, 15, 0, 15);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值