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;
}