http://blog.csdn.net/Apple_app/article/details/38867123
https://www.jianshu.com/p/c59a5c92f859
https://www.cnblogs.com/daxueshan/p/6742867.html
UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类。
使用UICollectionView 必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议。
下面给出一些常用方法,具体的使用可以参考Demo:点我下载苹果官方Demo:点我下载
[objc]view plaincopy
- (void)viewDidLoad
{
[superviewDidLoad];
self.title=@"UICollectionView学习";
//通过Nib生成cell,然后注册 Nib的view需要继承 UICollectionViewCell
[self.collectionViewregisterNib:[UINibnibWithNibName:@"SQCollectionCell"bundle:nil]forCellWithReuseIdentifier:kcellIdentifier];
//注册headerView Nib的view需要继承UICollectionReusableView
[self.collectionViewregisterNib:[UINibnibWithNibName:@"SQSupplementaryView"bundle:nil]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:kheaderIdentifier];
//注册footerView Nib的view需要继承UICollectionReusableView
[self.collectionViewregisterNib:[UINibnibWithNibName:@"SQSupplementaryView"bundle:nil]forSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:kfooterIdentifier];
//
self.collectionView.allowsMultipleSelection=YES;//默认为NO,是否可以多选
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -CollectionView datasource
//section
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView
{
return2;
}
//item个数
- (NSInteger)collectionView:(UICollectionView*)collectionViewnumberOfItemsInSection:(NSInteger)section
{
return6;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionViewcellForItemAtIndexPath:(NSIndexPath*)indexPath
{
//重用cell
UICollectionViewCell*cell = [collectionViewdequeueReusableCellWithReuseIdentifier:kcellIdentifierforIndexPath:indexPath];
//赋值
UIImageView*imageView = (UIImageView*)[cellviewWithTag:1];
UILabel*label = (UILabel*)[cellviewWithTag:2];
NSString*imageName = [NSStringstringWithFormat:@"%ld.JPG",(long)indexPath.row];
imageView.image= [UIImageimageNamed:imageName];
label.text= imageName;
cell.backgroundColor= [UIColorredColor];
returncell;
}
// The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
- (UICollectionReusableView*)collectionView:(UICollectionView*)collectionViewviewForSupplementaryElementOfKind:(NSString*)kindatIndexPath:(NSIndexPath*)indexPath{
NSString*reuseIdentifier;
if([kindisEqualToString: UICollectionElementKindSectionFooter ]){
reuseIdentifier = kfooterIdentifier;
}else{
reuseIdentifier = kheaderIdentifier;
}
UICollectionReusableView*view = [collectionView dequeueReusableSupplementaryViewOfKind :kindwithReuseIdentifier:reuseIdentifierforIndexPath:indexPath];
UILabel*label = (UILabel*)[viewviewWithTag:1];
if([kindisEqualToString:UICollectionElementKindSectionHeader]){
label.text= [NSStringstringWithFormat:@"这是header:%d",indexPath.section];
}
elseif([kindisEqualToString:UICollectionElementKindSectionFooter]){
view.backgroundColor= [UIColorlightGrayColor];
label.text= [NSStringstringWithFormat:@"这是footer:%d",indexPath.section];
}
returnview;
}
//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutsizeForItemAtIndexPath:(NSIndexPath*)indexPath
{
returnCGSizeMake(60,80);
}
//定义每个Section 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutinsetForSectionAtIndex:(NSInteger)section
{
returnUIEdgeInsetsMake(15,15,5,15);//分别为上、左、下、右
}
//返回头headerView的大小
-(CGSize)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutreferenceSizeForHeaderInSection:(NSInteger)section{
CGSize size={320,45};
returnsize;
}
//返回头footerView的大小
- (CGSize)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutreferenceSizeForFooterInSection:(NSInteger)section
{
CGSize size={320,45};
returnsize;
}
//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutminimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return10;
}
//每个item之间的间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
//{
// return 100;
//}
//选择了某个cell
- (void)collectionView:(UICollectionView*)collectionViewdidSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
UICollectionViewCell*cell = [collectionViewcellForItemAtIndexPath:indexPath];
[cellsetBackgroundColor:[UIColorgreenColor]];
}
//取消选择了某个cell
- (void)collectionView:(UICollectionView*)collectionViewdidDeselectItemAtIndexPath:(NSIndexPath*)indexPath
{
UICollectionViewCell*cell = [collectionViewcellForItemAtIndexPath:indexPath];
[cellsetBackgroundColor:[UIColorredColor]];
}