相关代理方法参考:http://www.cnblogs.com/wayne23/p/4013522.html
http://www.cnblogs.com/ios8/p/iOS-UICollectionView.html
a.使用xib创建cell和ReusableView:
1.添加对应的cell和ReusableView文件
2.在cell和ReusableView的xib文件中设置indentifier。
3.在viewDidLoad注册界面:
[self.collectionViewregisterNib:[UINibnibWithNibName:@"testCell"bundle:nil]forCellWithReuseIdentifier:@“cell的identifier"];
[self.collectionViewregisterNib:[UINibnibWithNibName:@"testHeadView"bundle:nil]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"ReusableView的identifier"];
4.生成cell的代理方法中实现自己想要的效果:
UICollectionViewCell *cell= [collectionViewdequeueReusableCellWithReuseIdentifier:cell的identifierforIndexPath:indexPath];
testCell *test=(testCell *)cell;
test.text=@"1!!!";
return cell;
5.在生成ReusableView的代理方法中实现自己想要的效果:
//生成头
if ([kindisEqualToString:UICollectionElementKindSectionHeader]) {
reuseIdentifier=@“ReusableView的identifier”;
UICollectionReusableView *view=[collectionViewdequeueReusableSupplementaryViewOfKind:kindwithReuseIdentifier:reuseIdentifierforIndexPath:indexPath];
testHeadView *testView=(testHeadView *)view;
testView.headLabel.text=@"这是文本1";
testView.headLabel2.text=@"这是文本2";
return view;
}
returnnil;
6.完成其他的基本代理
-------------------------------------------------------------------------------------------------------------------------------------
b.使用代码创建:
1.添加对应的cell和ReusableView文件(不生成xib)
2.在生成的.m文件中实现- (id)initWithFrame:(CGRect)frame方法:
//生成cell的自定义控件,ReusableView也是一样实现该方法,代码差不多
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColorpurpleColor];
self.imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(5,5,CGRectGetWidth(self.frame)-10,CGRectGetWidth(self.frame)-10)];
self.imgView.backgroundColor = [UIColorgroupTableViewBackgroundColor];
[selfaddSubview:self.imgView];
self.text = [[UILabelalloc]initWithFrame:CGRectMake(5,CGRectGetMaxY(self.imgView.frame),CGRectGetWidth(self.frame)-10,20)];
self.text.backgroundColor = [UIColorbrownColor];
self.text.textAlignment =NSTextAlignmentCenter;
[selfaddSubview:self.text];
self.btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
self.btn.frame =CGRectMake(5,CGRectGetMaxY(self.text.frame),CGRectGetWidth(self.frame)-10,30);
[self.btnsetTitle:@"按钮"forState:UIControlStateNormal];
self.btn.backgroundColor = [UIColororangeColor];
[selfaddSubview:self.btn];
}
returnself;
}
3.在viewDidLoad注册自定义的cell://注册cell和ReusableView(相当于头部),ps:注册代码跟有xib的不一样
[self.collectionViewregisterClass:[myCellclass]forCellWithReuseIdentifier:@"cell"];
[self.collectionViewregisterClass:[UICollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"ReusableView"];
4.在对应的生成方法中填充,生成/填充自定义cell的代码:-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString *identify =@"cell";
myCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:identifyforIndexPath:indexPath];
[cell sizeToFit];
cell.text.text = [NSStringstringWithFormat:@"Cell %ld",indexPath.row];
return cell;
}
5.完成其他的基本代理