iOS 关于UICollectionView的headerView的问题、cell间隙问题/pageEnabled显示偏移问题_minimuminteritemspacing 为0 collectionview依然出现竖直白线-程序员宅基地

技术标签: UICollectionView  iOS/oc  headerView  

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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u011146511/article/details/51147900

智能推荐

百度c语言贴吧 经典C源程序100例-2_c语言贴吧顶贴机源码-程序员宅基地

文章浏览阅读1k次。【程序2】 题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高    于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提    成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于    40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5_c语言贴吧顶贴机源码

偏向锁,轻量级锁与重量级锁_轻量级锁和偏向锁,重量级锁-程序员宅基地

文章浏览阅读256次。偏向锁Hotspot 的作者经过以往的研究发现大多数情况下锁不仅不存在多线程竞争,而且总是由同一线程多次获得,为了让线程获得锁的代价更低而引入了偏向锁。当一个线程访问同步块并获取锁时,会在对象头和栈帧中的锁记录里存储锁偏向的线程 ID。以后该线程在进入和退出同步块时不需要花费CAS操作来加锁和解锁,而只需简单的检验一下对象头的Mark Word里是否存储着指向当前线程的偏向锁,如果存在,表示线_轻量级锁和偏向锁,重量级锁

TMS320C6678开发例程使用手册(7)_sa tic fir-程序员宅基地

文章浏览阅读608次。3.算法Demo例程演示所有工程均位于光盘"Demo\Algorithm\Application"文件夹内,本章节例程仅演示算法功能,算法与平台和系统无关,如需使用SYS/BIOS请自行移植。使用CCS工程导入和编译步骤导入例程。备注:本节提供的例程测试结果仅供参考。3.1FIR——有限长单位冲激响应滤波器此程序的作用是进行FIR有限长单位冲激响应滤波器测试。按照工程导入步骤加载FIR.out文件,然后点击程序运行按钮,程序会在断点处停下来。点击CCS菜单"Tools->Gra_sa tic fir

Spring AOP配置标签解析_spring-aop:5.0.8.release-程序员宅基地

文章浏览阅读1.2k次。准备Spring版本:5.0.8解析过程开启 Spring AOP 注解自动代理需要配置标签 &amp;amp;amp;amp;amp;lt;aop:aspectj-autoproxy/&amp;amp;amp;amp;amp;gt;,查看 spring-aop:5.0.8.RELEASE\META-INF\spring.handlers 文件,配置以下内容:http\://www.springframework.org/schema/aop=org.sp..._spring-aop:5.0.8.release

git命令大全_git diff testtag head --name-only | xargs tar-程序员宅基地

文章浏览阅读9.2k次。git操作配置.gitignore文件作用:可以屏蔽掉某些不需要被管理的文件或文件夹,如日志文件、临时文件等。git rm -r --cached .git add .git commit -m ‘update .gitignore’新建创建一个新的git仓库。这个版本库的配置、存储等信息会被保存到.git文件中# 初始化当前项目$ git init# 新建一个目录,将其初始化为Git代码库$ git init [project-name]# 在指定目录创建一个空的 Git _git diff testtag head --name-only | xargs tar

悲催的程序员,以及程序员的悲催-程序员宅基地

文章浏览阅读458次。我尽量用平和一点的口吻跟你说说关于程序员的那点事儿。1.我在一个叫摩托罗拉的公司干过,那地方有50%的人整天干的事情就是催另外25%的人没完没了的解剩下那25%的人造成的bug。我是个程序员,每天敲敲打打,哪天电脑崩溃了你会发现我这辈子啥都没留下。大多数人甚至都没有想过我们是怎么把手机捣鼓出来的,包括是是否人手一套乐高的家庭套装工具。我那可爱的岳父岳母在向自己的亲戚朋友们介绍我的时候,总是用一种充满自豪的口吻轻描淡写的说,他在摩托罗拉上班(我离开摩托罗拉以后他

随便推点

centos7 mysql允许连接_centos7 mysql允许远程连接设置-程序员宅基地

文章浏览阅读779次。Mysql为了安全性,在默认情况下用户只允许在本地登录,可是在有此情况下,还是需要使用用户进行远程连接,因此为了使其可以远程需要进行如下操作:一、允许root用户在任何地方进行远程登录,并具有所有库任何操作权限,具体操作如下:在本机先使用root用户登录mysql:mysql -u root -p"youpassword"进行授权操作:mysql>GRANT ALL PRIVILEGES O..._centos7 mysql 允许客户端连接

【小Y学算法】️每日LeetCode打卡️——40.二叉树的后序遍历-程序员宅基地

文章浏览阅读1.3w次,点赞24次,收藏60次。没有特别幸运,那么请先特别努力,别因为懒惰而失败,还矫情地将原因归于自己倒霉。所以说,树倒了,没有一片雪花是无辜的

搬砖:ioctl函数,可以获取ip地址,修改ip地址,网卡地址等_if(ioctl(if_fd, i_push, "ip") < 0){-程序员宅基地

文章浏览阅读482次。Android配置ip地址ioctl函数,可以获取ip地址,修改ip地址,网卡地址等部分转自http://www.cnblogs.com/zht-blog/p/4025903.htmlint ioctl(int d, int request, ...);ioctl用来控制特殊设备文件的属性,第一个参数fd必须是一个已经打开的文件描述符,第三个参数一般为cha..._if(ioctl(if_fd, i_push, "ip") < 0){

USB-C 端口在您的 Mac 上无法使用如何解决?_macos 使用usbc 转接头 会提示 winusb-程序员宅基地

文章浏览阅读266次。由于 Mac 上缺少 USB-C 端口,即使一个端口停止工作,对许多用户来说也可能很麻烦。如果 MacBook 上的 USB-C 端口突然停止工作,请查看以下解决问题的一些可能解决方案。如何修复 USB-C 端口在您的 MacBook 上不工作重启你的 Mac如果 MacBook 上的 USB-C 端口突然停止工作,最简单的修复方法就是重新启动 Mac。在大多数情况下,这应该足以解决手头的问题并确保 MacBook 上的 USB-C 端口再次工作。更新到最新的 macOS 版本如果您的 MacB_macos 使用usbc 转接头 会提示 winusb

【计算机毕业设计】基于 SpringBoot+SpringCloud+Vue 电商秒杀系统-程序员宅基地

文章浏览阅读18次。基于 SpringBoot+SpringCloud+Vue 电商秒杀系统

C++编程规范---第9章 类的构造函数、析构函数与赋值函数_c++析构函数规范-程序员宅基地

文章浏览阅读574次。Blogdown--构造函数、析构函数与赋值函数是每个类最基本的函数。它们太普通以致让人容易麻痹大意,其实这些貌似简单的函数就象没有顶盖的下水道那样危险。 每个类只有一个析构函数和一个赋值函数,但可以有多个构造函数(包含一个拷贝构造函数,其它的称为普通构造函数)。对于任意一个类A,如果不想编写上述函数,C++编译器将自动为A产生四个缺省的函数,如 A(void);_c++析构函数规范

推荐文章

热门文章

相关标签