iOS 关于UICollectionView的headerView的问题、cell间隙问题/pageEnabled显示偏移问题

1.collectionView的注意事项:必须注册cell;如果在storyboard添加了可重用标示符,可以不注册.

             必须实现代理方法.否则会报错.

             当cell的大小显示不正常的时候,可以试一下在视图加载完成之后,将要出现的时候,设置itemSize;  注册cell的方法会跳到dequeueReusableCellWithReuseIdentifier:(缓存池中调用cell),如果缓存池中没有会创建cell,这时候会自动调用 -(instancetype)initWithFrame:(CGRect)frame这个方法.

在storyboard 中设置cell的颜色不起作用,要在控制器中用代码设置.

 

2.collectionView纯代码设置组头:

-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

{

    if(section == 0)

    {

        CGSize size = {320, 150};

        return size;

    }    else

    {

        CGSize size = {320, 50};

        return size;

    }

 

}

 

3.

#warning 如果在storboard中设置了组头或组尾,必须设置重用标识符

    

    //这个方法必须要在storyboard中设置可视化header,footer,可重用标示符,才会起作用

//    static NSString *headerIdentifier = @"header";

    UICollectionReusableView *resuableView;

//    headerIdentifier = (kind == UICollectionElementKindSectionHeader) ? @"header" : @"footer";

    if(kind ==UICollectionElementKindSectionHeader){

    resuableView = [collectionView dequeueReusableSupplementaryViewOfKind:kindwithReuseIdentifier:@"header"forIndexPath:indexPath];

//            return resuableView;

    }else{

        resuableView = [collectionView dequeueReusableSupplementaryViewOfKind:kindwithReuseIdentifier:@"footer"forIndexPath:indexPath];

//        return resuableView;

 

    }

    return resuableView;

    

}

 

4.    2的方法和3的方法同时使用会报错.

 

 

========

//+++++++++++++++++

//使用collectionview的header和foot时,自定义的headerview和footview要继承自继承UICollectionReusableView;

//注册headerView Nib的view需要继承UICollectionReusableView

[self.collectionView registerNib:[UINib nibWithNibName:@"SQSupplementaryView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kheaderIdentifier];

//注册footerView Nib的view需要继承UICollectionReusableView

[self.collectionView registerNib:[UINib nibWithNibName:@"SQSupplementaryView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kfooterIdentifier];

 

//这个是没有xib的注册组头 

 

[collec registerClass:[LYHomephoneczHeaderviewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:identifierheader];

 

//+++++++++++++++++

=========自定义的组头组尾设置,缓存池复用,-----必须先在前面注册

 

//设置headview

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

    

    if (indexPath.section == 0) {

        

        SPStoreHeadView* headView;

        if ([kindisEqual:UICollectionElementKindSectionHeader]) {

            headView = (SPStoreHeadView*)[self.collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:collectionHeadViewCellIdentifierforIndexPath:indexPath];

            //别在这对headView坐标做处理

        }

        headView.delegate =self;

        [headView initDataWithStore:self.store];

        return headView;

    }else{

        

        SPProduct* product =self.products[indexPath.section];

        SPCategoryHeadView* headView;

        if ([kindisEqual:UICollectionElementKindSectionHeader]) {

            headView = (SPCategoryHeadView*)[self.collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:collectionTitleViewCellIdentifierforIndexPath:indexPath];

            //别在这对headView坐标做处理

        }

        

        NSString* title = product.goodsName;

        [headView setLabelText:title];

        

        return headView;

    }

    

}

 

****************cell间隙问题:

解决方法一:这个方法只能设置间隙为0

flowlayout.minimumInteritemSpacing=0;//这个必须设置才能保证没有间隙

//cell大小

-(CGSize)collectionView:(UICollectionView* )collectionView layout:(UICollectionViewLayout* )collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

 

NSInteger num = 2;

CGFloat width = CGRectGetWidth(collectionView.bounds)/num;

CGFloat height = 257;

 

if(indexPath.row == 0){

    //第一列的width比其他列稍大一些,消除item之间的间隙

    CGFloat realWidth = CGRectGetWidth(collectionView.bounds) - floor(width) * (num - 1);

    return CGSizeMake(realWidth, height);

}else{

    return CGSizeMake(floor(width), height);

}

}

 

 

解决方法二:这个既可以设置间隙为0,也可以设置间隙大小

自定义一个UICollectionViewLayout继承自UICollectionViewFlowLayout,然后重写-layoutAttributesForElementsInrect:方法

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {

    NSArray *answer = [super layoutAttributesForElementsInRect:rect];

    for(int i = 1; i < [answer count]; ++i) {

        UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];

        UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1];

        NSInteger maximumSpacing = 0;

        NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);

        

        if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {

            CGRect frame = currentLayoutAttributes.frame;

            frame.origin.x = origin + maximumSpacing;

            currentLayoutAttributes.frame = frame;

        }

    }

    return answer;

}

参考:https://www.jianshu.com/p/4db0be2f4803

 

************collectionview的pageENabled偏移问题:

UICollectionView实现水平滑动 pagingEnabled分页偏移问题

创建UICollectionViewFlowLayout

要设置flowLayout.minimumLineSpacing = 0.000001f;

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.minimumLineSpacing = 0.000001f;
    //flowLayout.minimumInteritemSpacing = 20;
    //flowLayout.itemSize = CGSizeMake(YCScreenWidth, YCScreenHeight);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
代理方法

#pragma mark <UICollectionViewDelegateFlowLayout>
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.bounds.size.width, self.bounds.size.height);
}


类似解决UITableView分组 组头视图默认高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.000001f;
}

可以使用以下代码添加UICollectionViewheaderView: ``` - (void)viewDidLoad { [super viewDidLoad]; // 初始化UICollectionViewFlowLayout UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.itemSize = CGSizeMake(100, 100); layout.minimumLineSpacing = 10; layout.minimumInteritemSpacing = 10; layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 50); // 初始化UICollectionView UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout]; collectionView.dataSource = self; collectionView.delegate = self; [self.view addSubview:collectionView]; // 注册UICollectionViewCell [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; // 注册UICollectionReusableView [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"]; } #pragma mark - UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 10; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; cell.backgroundColor = [UIColor redColor]; return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath]; headerView.backgroundColor = [UIColor blueColor]; return headerView; } return nil; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值