iOS开发之控件UICollectionView 的使用

在iOS开发的过程中,很多地方需要使用到列表。UITableView 和UICollectionView。记录一下留个笔记。

下面一段是来自其他大神的讲解UICollectionView详解引用一下。

UICollectionView,内容和布局完全分离的设计,UICollectionView负责界面部分,UICollectionViewlayout负责UIcollectionView的布局,具体的每个元素的布局就交给UICollectionViewLayoutAttributes,另外attributes也是可以进行扩展的,比如需要加入maskView或者改变layer的属性,都可以在attributes里面进行自己的定义。


UICollectionView 一般使用的属性

- (UICollectionView *)productCollectionView
{
    if (!_productCollectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumLineSpacing = 5;  //行间距
        flowLayout.minimumInteritemSpacing = 5; //列间距
        flowLayout.estimatedItemSize = CGSizeMake(50, 50);  //预定的itemsize
        flowLayout.itemSize = CGSizeMake(150, 100); //固定的itemsize
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;//滑动的方向 垂直
        //初始化 UICollectionView
        _productCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _productCollectionView.delegate = self; //设置代理
        _productCollectionView.dataSource = self;   //设置数据来源
        _productCollectionView.backgroundColor = kSDYBgViewColor;

        _productCollectionView.bounces = YES;   //设置弹跳
        _productCollectionView.alwaysBounceVertical = YES;  //只允许垂直方向滑动
        //注册 cell  为了cell的重用机制  使用NIB  也可以使用代码 registerClass xxxx
        [_productCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ProductCollectionCell class]) bundle:nil] forCellWithReuseIdentifier:MyRecordCollectionCellIdentifier];
    }
    return _productCollectionView;
}
对某个cell 自定义设置size ,间距 

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;//可以根据section 设置不同section item的行间距
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;// 可以根据section 设置不同section item的列间距
    
    if (section == 5) return 10;
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        return CGSizeMake(50, 50);
    }
    if (indexPath.section == 2 && indexPath.row == 3) {
        return CGSizeMake(80, 80);
    }
    return CGSizeMake(100, 100);//可以根据indexpath 设置item 的size
}
cell的点击事件

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
}
DataSource 

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;//设置section 个数
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;//根据section设置每个section的item个数
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //cell的重用,在设置UICollectionView 中注册了cell
    ProductCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MyRecordCollectionCellIdentifier forIndexPath:indexPath];
    cell.productModel = self.dataSource[indexPath.row];//设置cell的数据
    return cell;
}

- (void)reloadData; 重新加载数据

以上是UICollectionView 的简单使用

多选

@property (nonatomic) BOOL allowsSelection; // default is YES   允许选择
@property (nonatomic) BOOL allowsMultipleSelection; // default is NO
@property (nonatomic, readonly, nullable) NSArray<NSIndexPath *> *indexPathsForSelectedItems; // returns nil or an array of selected index paths
#else
- (nullable NSArray<NSIndexPath *> *)indexPathsForSelectedItems; // returns nil or an array of selected index paths
#endif
- (void)selectItemAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition;
- (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

UICollectionViewLayoutAttributes使用UICollectionViewLayout布局详解












  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值