如何纯代码给UICollectionViewwebview 添加headerrView

oc基础(19)
大家都说colletionView和UITabbleView 是兄弟,而且colletionView是在IOS 6之后出来的, colletionView和UITabbleView他俩确实是兄弟,但是使用的时你回遇到好多坑。
UICollectionView
*colletionView = [[UICollectionView alloc]init];初始化一个colletionView,如果你这么搞,你就掉到坑里了。应为人家官方文档是这样给你的。
so!!!!你这样搞就崩掉。你只能这样。
UICollectionViewFlowLayout *grid =[[UICollectionViewFlowLayout alloc] init]; grid.itemSize = CGSizeMake(80, 80); //设置colletionView的大小
grid.sectionInset = UIEdgeInsetsMake(10.0, 10, 10, 10);
UICollectionView *colletionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:grid];
colletionView.delegate =self;
colletionView.dataSource = self;
[colletionView registerClass:[photoCell class] forCellWithReuseIdentifier:@"simpleCell"];//这个一定要加不加上的化你的Cell init 是不会调用的哦 !!
OK 正就成功实例化了一个colletionView它与它的兄弟一样需要设置代理,设置数据源。在实例化的时候已经设置好了。
现在去实现他的代理并且自定义一个colletionViewCell
自定义Cell 要去自定义一个Cell类继承UICollectionViewCell
#import &UIKit/UIKit.h&
@interface Cell : UICollectionViewCell
@property (nonatomic,strong)UIImageView *
@implementation Cell
-(id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
self.image = [[UIImageView alloc]init];
[self.image setFrame:CGRectMake(self.contentView.frame.size.width/2+20+5, -5, 20, 20)];
[self.image setBackgroundColor:[UIColor redColor]];
self.image.layer.cornerRadius=self.image.frame.size.width/2;
self.image.layer.masksToBounds=YES;
[self addSubview:self.image];
return self;
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
return 10;
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
static NSString *cellIdentifier = @"simpleCell";
Cell *cell = (Cell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell ==nil) {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:17084次
排名:千里之外
原创:32篇
转载:10篇
(1)(1)(1)(1)(1)(1)(5)(1)(1)(2)(1)(2)(5)(6)(2)(2)(6)(3)iOS&swift&纯代码编写&collectionView_blesssecret_新浪博客
iOS&swift&纯代码编写&collectionView
CollectionView是iOS
中比较常用的控件,我们常用的照片库,还有那个估值已经50亿美金的pinterest的主界面都是用这个控件,今天我要用纯代码手写CollectionView。一般来说大家做iOS都是用拖拽的办法来拉控件到storyboard,不过这种方法的屏幕适配不好控制,(autoLayout难以处理比较复杂的界面),更重要的是如果你想有比较复杂的功能,则必须手写。
首先新建一个swift的project,在class里面我们加入三个subClass,UICollectionViewDelegate,
UICollectionViewDatasource,
UICollectionViewDelegateFlowLayout,再申明以下的全局变量
​相比Object-C,我们发现申明变量是用var和let。在swift,var是申明变量,这种变量和其他的语言不一样,这种变量是指我可以修改对象的类型;比如我一开始申明myCollecTionView是一个CollectionView,但是之后的程序我可以把他改成一个TableView,完全没有问题。let申明的是常量,这个常量也和C++,JAVA那种语言不一样,比如我同样用let申明myCollectionView,里面的各种值是可以随意修改的,唯一不能改的就是这个变量的Type(类型)。为了安全起见,能用let尽量用let,这里我是测试,所以用了var。
还有一个与众不同的地方是哪个感叹号!,除此之外还有一个问号?,这个解释比较复杂,不过我们可以这么认为,如果是感叹号,那么意味着这个变量是一定存在的,如果是问号,意味这个变量可以存在,也可以是空。所以他们的意思是require和option。至于哪里用!哪里用?,大家自己试试,一般都可以看到自动补全里面会写上。另外比较头疼的是,苹果swift语言的变化很大,变化最多的就是这两个符号了;经常出现昨天写的代码可以运行,今天升级xcode,代码就全是编译错误了。。。遇到此类问题,请Google苹果开发文档。目前我使用的编译器是xcode
6.3. 详细资料请看:&
/swift/7.html​
变量申明完了之后,我们开始写函数,如果大家细心的话就会发现,当我们把UICollectionViewDelegate和Datasource加到继承的class的时候,编译器会报错。这不是编译器疯了(虽然它经常疯),而是我们有两个require的function没有写。先不管它,我们先初始化下所有的变量。
​这里很多代码看英文就知道是什么意思,不过还是有几处需要注意:第一myCollectionView
如果不想使用默认的版面的话,那么初始化必须像我这么写,把flowLayout加进去。同样要注意,如果我们有两个collectionView,那么必须申明两个flowLayout,否则两个版面会相互干扰。第二,registerClass函数是干嘛的?这个是用来注册collectionView里面的cell。如果我们用的是拖拽了一个collectionview到画板上吗,那么registerClass就干了下面两件事。
由于我们自定义了一个CollectionViewCell,那么再创建一个class继承UICollectionViewCell
​代码就这样写:
​一切辅助工作完成,最后写最重要的两个func
这里我放了个错误,我只把图片复制进了这个project,而没有加入到图片库里面去,结果图片显示不出来了。。。
copy过来的图片文件夹
到我们项目的general设置界面去
点那个灰色的-&箭头
​再把文件夹拖过来
一切完成,运行一下
我们看到是这得界面,图片是不是太小了点
这个时候,我们的flowLayout就派上用场了,添加一个func​
这个flowLayout简单的可以写成我这样的,一行放三个。如果复杂像pinterest那种界面就要做大量的数学计算才可以。交给以后的大神去做吧!
再运行一次,不错就这样了。
本次所有的源码:/blesssecret/iOS-swift-developing/tree/master/collectionView1
blesssecret
博客等级:
博客积分:0
博客访问:6,959
关注人气:0
荣誉徽章:纯代码创建UICollectionView步骤以及简单使用 - 简书
纯代码创建UICollectionView步骤以及简单使用
UICollectionView主要用于瀑布流,由于一直接触较少,每次需要使用的时候都从网上翻阅资料,此次自己总结整理,以备不时之需。
collectionView和tableView最大的不同之处就是需要自定义cell,所以第一步自定义collectionViewCell
#import &UIKit/UIKit.h&
@interface MyCollectionViewCell : UICollectionViewCell
@property (strong, nonatomic) UIImageView *topI
@property (strong, nonatomic) UILabel *
#import "MyCollectionViewCell.h"
@implementation MyCollectionViewCell
- (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
= [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 70, 70)];
_topImage.backgroundColor = [UIColor redColor];
[self.contentView addSubview:_topImage];
_botlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 70, 30)];
_botlabel.textAlignment = NSTextAlignmentC
_botlabel.textColor = [UIColor blueColor];
_botlabel.font = [UIFont systemFontOfSize:15];
_botlabel.backgroundColor = [UIColor purpleColor];
[self.contentView addSubview:_botlabel];
在viewController中实现collectionView的三个协议
UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout
具体实例化步骤均在代码中有注释,如下
#import "CollectionViewController.h"
#import "MyCollectionViewCell.h"
@interface CollectionViewController ()&UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout&
UICollectionView *mainCollectionV
@implementation CollectionViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
//1.初始化layout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//设置collectionView滚动方向
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
//设置headerView的尺寸大小
layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 100);
//该方法也可以设置itemSize
layout.itemSize =CGSizeMake(110, 150);
//2.初始化collectionView
mainCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
[self.view addSubview:mainCollectionView];
mainCollectionView.backgroundColor = [UIColor clearColor];
//3.注册collectionViewCell
//注意,此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
[mainCollectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
//注册headerView
此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致
均为reusableView
[mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
//4.设置代理
mainCollectionView.delegate =
mainCollectionView.dataSource =
#pragma mark collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
cell.botlabel.text = [NSString stringWithFormat:@"{%ld,%ld}",(long)indexPath.section,(long)indexPath.row];
cell.backgroundColor = [UIColor yellowColor];
//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
return CGSizeMake(90, 130);
//footer的size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
return CGSizeMake(10, 10);
//header的size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
return CGSizeMake(10, 10);
//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
return UIEdgeInsetsMake(10, 10, 10, 10);
//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
return 10;
//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
return 15;
//通过设置SupplementaryViewOfKind 来设置头部或者底部的view,其中 ReuseIdentifier 的值必须和 注册是填写的一致,本例都为 “reusableView”
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];
headerView.backgroundColor =[UIColor grayColor];
UILabel *label = [[UILabel alloc] initWithFrame:headerView.bounds];
label.text = @"这是collectionView的头部";
label.font = [UIFont systemFontOfSize:20];
[headerView addSubview:label];
return headerV
//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
NSString *msg = cell.botlabel.
NSLog(@"%@",msg);
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
完成以上两步,效果图如下:

我要回帖

更多关于 wkwebview 添加header 的文章

 

随机推荐