美文网首页
UICollectionView使用,自定义cell,表头,表尾

UICollectionView使用,自定义cell,表头,表尾

作者: ly_chee_ | 来源:发表于2020-06-15 10:17 被阅读0次

    初始化layout

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    layout.itemSize=CGSizeMake(100 ,200  );    //cell宽高

    layout.sectionInset = UIEdgeInsetsMake(10, 10 , 10, 10);    //cell间距

    layout.minimumLineSpacing = 10;    //跟滚动方向相同的间距

    layout.minimumInteritemSpacing = 5;    //跟滚动方向垂直的间距

    创建collectionView

    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-NAVH) collectionViewLayout:_layout];

    collectionView.dataSource = self;

    collectionView.delegate = self;
    [collectionView registerClass:UICollectionViewCell.class forCellWithReuseIdentifier:@"cell"];    

    //  自定义cell
    //[collectionView registerClass:SquareImageCollectionCell.class forCellWithReuseIdentifier:@"SquareImageCollectionCell"];      

    [collectionView registerClass:[MyFootListView class] forSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:@"MyFootListView"];     //  代码初始化表头

    [self.view addSubview:collectionView];

    代理中方法

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

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

    //自定义cell

    // SquareImageCollectionCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"SquareImageCollectionCell" forIndexPath: indexPath];
    //cell.cellImg = @"image";

    return cell;

    }

    //组数

    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

        return 1;

    }

    //行数

    - (NSInteger)collectionView:(UICollectionView*)collectionViewnumberOfItemsInSection:(NSInteger)section {

        return  6;

    }

    // 要先设置表头大小

    - (CGSize)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutreferenceSizeForHeaderInSection:(NSInteger)section {

        return  CGSizeMake(320, 50);

    }

    // 创建一个继承collectionReusableView的类,用法类比tableViewcell

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

        UICollectionReusableView *reusableView = nil;

        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

            //  自定义头部视图 ,需要继承UICollectionReusableView

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

     reusableView = headerView;

        }else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {

            // 自定义底部视图,同头部视图

        }

        return  reusableView;

    }

    //cell点击方法

    - (void)collectionView:(UICollectionView*)collectionViewdidSelectItemAtIndexPath:(NSIndexPath*)indexPath{

    }

    相关文章

      网友评论

          本文标题:UICollectionView使用,自定义cell,表头,表尾

          本文链接:https://www.haomeiwen.com/subject/orhcxktx.html