分组的自定义流布局方法一
来源:互联网 发布:avmoo最新域名2016 8 编辑:程序博客网 时间:2024/05/29 11:16
在iOS开发流布局出来的界面很好看,但是要每一个collectionView都只有一个layout,所以分组自定义 没有那么容易,下面我来说说第一种方法。
由于文件较多,我这里只介绍重点内容,其他的内容就不多做介绍了,大家可以下载项目自己去查看。
下载地址:https://github.com/markwangjy/SectionCollectionView.git
重点知识点:建一个继承UICollectionViewCell的BaseCollectionViewCell,然后再这个cell里面添加uicollectionView
具体做法:
.h文件中:
#import <UIKit/UIKit.h>
#import "SectionView.h"
@interface BaseCollectionViewCell :UICollectionViewCell <UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) SectionView *headerView;
@property (nonatomic,strong) UICollectionView *contentCollectionView;
@property (nonatomic,weak) UICollectionViewFlowLayout *contentLayout;
#pragma mark - 子类重构
-(void)initCompletionOpration;
-(void)contentModelCompletionOpration;
@property(nonatomic,assign)NSInteger section;
@end
.m文件中#import "BaseCollectionViewCell.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGTH [UIScreen mainScreen].bounds.size.height
@implementation BaseCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame{
self = [superinitWithFrame:frame];
if (self) {
[selfinitSubViews];
}
returnself;
}
-(UICollectionView *)contentCollectionView{
if (!_contentCollectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayoutalloc]init];
_contentCollectionView =[[UICollectionViewalloc]initWithFrame:CGRectZerocollectionViewLayout:layout];
_contentCollectionView.backgroundColor = [UIColorwhiteColor];
_contentCollectionView.scrollEnabled =NO;
_contentCollectionView.showsVerticalScrollIndicator =NO;
_contentCollectionView.showsHorizontalScrollIndicator =NO;
_contentCollectionView.delegate =self;
_contentCollectionView.dataSource =self;
_contentLayout = layout;
}
return_contentCollectionView;
}
-(void)initSubViews{
[selfaddSubview:self.contentCollectionView];
[selfaddSubview:self.headerView];
[selfinitSubViewLayouts];
[selfinitCompletionOpration];
[selfcontentModelCompletionOpration];
}
-(void)setSection:(NSInteger)section{
self.headerView.contentLabel.text = [NSString stringWithFormat:@"这里是第%ld组的头部",section];
}
- (void)initSubViewLayouts{
self.headerView.frame =CGRectMake(0,0, self.bounds.size.width,35);
self.contentCollectionView.frame = CGRectMake(0,35, self.bounds.size.width,self.bounds.size.height-35);
}
-(void)initCompletionOpration{
}
-(void)contentModelCompletionOpration{
}
- (SectionView *)headerView{
if (!_headerView) {
_headerView = [[SectionViewalloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,35)];
}
return_headerView;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return12;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
returnnil;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
[collectionView deselectItemAtIndexPath:indexPathanimated:true];
}
@end
做出了基本collectionViewcell的配置之后,在主界面调用加载collectionView的cell都继承上面这个cell。就可以实现每个分组中完全自定义。下面是效果图:
下面是下载地址:https://github.com/markwangjy/SectionCollectionView.git
这是一种方法,比较简单,消耗相对来说比较大,过两天我会再分析一下另一种方法。
谢谢大家阅读,我是ios_mark qq:1124728522 欢迎大家多多批评指正。
- 分组的自定义流布局方法一
- 分组的自定义流布局方法二
- 自定义流布局FloatLayout(一)
- 自定义流布局FlowLayout
- Android 自定义流布局
- 自定义流布局FlowGroup
- 常用的自定义View例子一(流布式布局)
- 瀑布流布局的实现(一)
- 自定义标签布局(流布局)
- 自定义组件---瀑布流布局
- 自定义实现瀑布流布局
- 瀑布流布局与自定义瀑布流布局插件
- 实现瀑布流布局的几种方法
- 自定义view/viewgroup/流布局(极简版)
- iOS 自定义流布局 进阶篇
- 自定义UICollectionViewFlowLayout进行瀑布流布局
- Android 自定义View实现文本流布局
- 自定义标签布局(流布局)
- 3DGIS第四章 视点相关的实时选择性细化
- Spring boot上配置与使用mybatis plus
- 8大排序算法图文讲解
- Support V4 包大拆分
- centos急速搭建lamp
- 分组的自定义流布局方法一
- AfxBeginThread创建线程并指定优先级
- 16 - 12 - 27 图的遍历-深度优先遍历(DFS)
- Manven+Eclipse搭建面向服务式架构
- Android-app-DwWeather天气预报
- 养成这五个习惯,成为资深设计师(二)
- Error:Cause: com.android.sdklib.repository.FullRevision
- 3DGIS第五章 基于GPU的优化处理
- UIWebView长按保存图片